Bug#620179: Sorted entries

2019-03-01 Thread Heiko
Hello *,

I made a fix by sorting the read lines from /proc/diskstats.

Diff:

703c703
<   char line[256], dev_name[MAX_NAME_LEN], dev_name2[MAX_NAME_LEN];
---
>   char line[256], dev_name[MAX_NAME_LEN];
713,717d712
< char *lines[256]; /* Array of 256 pointers to line-strings */
< int line_len;
< int line_nb=0; /* will contain the line (device) count */
< int l_c, l_c2;
< char *tmp;
724,725c719
< /* /proc/diskstats contains an unordered list of devices */
<   if ((fp = fopen(DISKSTATS, "r")) == NULL) 
---
>   if ((fp = fopen(DISKSTATS, "r")) == NULL)
727,733d720
<   while (fgets(line, sizeof(line), fp) != NULL && line_nb < 256) {
<   line_len=strlen(line);
<   lines[line_nb]=malloc(line_len+1); /* Make room for the 
terminating \0 */
<   memcpy(lines[line_nb], line, line_len+1); /* memcpy is faster 
than strcpy */
<   line_nb++; 
< }
<   fclose(fp);
735,754c722,723
< /* Sort device list */
< for (l_c=0; l_c < line_nb-1; l_c++)
< {
<  sscanf(lines[l_c], "%u %u %s", , , dev_name);
<  for (l_c2=l_c+1; l_c2 < line_nb; l_c2++)
<  {
<  sscanf(lines[l_c2], "%u %u %s", , , dev_name2);
<  if (strncmp(dev_name, dev_name2, sizeof(line)) > 0) { // 
swap places
< tmp=lines[l_c]; 
< lines[l_c]=lines[l_c2];   
< lines[l_c2]=tmp;
< l_c--; /* walk thru it again */
   while (fgets(line, sizeof(line), fp) != NULL) {
> 
756c725
<   i = sscanf(lines[l_c], "%u %u %s %lu %lu %lu %lu %lu %lu %lu %u 
%u %u %u %lu %lu %lu %lu",
---
>   i = sscanf(line, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %u %u %u 
> %u %lu %lu %lu %lu",
828,831c797
< 
   fclose(fp);

Maybe more readable: the whole function…

/*
 ***
 * Read stats from /proc/diskstats.
 *
 * IN:
 * @currIndex in array for current sample statistics.
 ***
 */
void read_diskstats_stat(int curr)
{
FILE *fp;
char line[256], dev_name[MAX_NAME_LEN], dev_name2[MAX_NAME_LEN];
char *dm_name;
struct io_stats sdev;
int i;
unsigned int ios_pgr, tot_ticks, rq_ticks, wr_ticks;
unsigned long rd_ios, rd_merges_or_rd_sec, rd_ticks_or_wr_sec, wr_ios;
unsigned long wr_merges, rd_sec_or_wr_ios, wr_sec;
unsigned long dc_ios, dc_merges, dc_sec, dc_ticks;
char *ioc_dname;
unsigned int major, minor;
char *lines[256]; /* Array of 256 pointers to line-strings */
int line_len;
int line_nb=0; /* will contain the line (device) count */
int l_c, l_c2;
char *tmp;

memset(, 0, sizeof(struct io_stats));

/* Every I/O device entry is potentially unregistered */
set_entries_unregistered(iodev_nr, st_hdr_iodev);

/* /proc/diskstats contains an unordered list of devices */
if ((fp = fopen(DISKSTATS, "r")) == NULL) 
return;
while (fgets(line, sizeof(line), fp) != NULL && line_nb < 256) {
  line_len=strlen(line);
  lines[line_nb]=malloc(line_len+1); /* Make room for the 
terminating \0 */
  memcpy(lines[line_nb], line, line_len+1); /* memcpy is faster 
than strcpy */
  line_nb++; 
}
fclose(fp);

/* Sort device list */
for (l_c=0; l_c < line_nb-1; l_c++)
{
 sscanf(lines[l_c], "%u %u %s", , , dev_name);
 for (l_c2=l_c+1; l_c2 < line_nb; l_c2++)
 {
 sscanf(lines[l_c2], "%u %u %s", , , dev_name2);
 if (strncmp(dev_name, dev_name2, sizeof(line)) > 0) { // swap 
places
tmp=lines[l_c]; 
lines[l_c]=lines[l_c2];   
lines[l_c2]=tmp;
l_c--; /* walk thru it again */
   break;
 }
 }
}

/* Walk through device list */
for (l_c=0; l_c < line_nb; l_c++)
{
/* major minor name rio rmerge rsect ruse wio wmerge wsect wuse 
running use aveq dcio dcmerge dcsect dcuse*/
i = sscanf(lines[l_c], "%u %u %s %lu %lu %lu %lu %lu %lu %lu %u 
%u %u %u %lu %lu %lu %lu",
   , , dev_name,
   _ios, _merges_or_rd_sec, _sec_or_wr_ios, 
_ticks_or_wr_sec,
   _ios, _merges, _sec, _ticks, _pgr, 

Bug#620179: Sorted Entries

2019-01-16 Thread Patrik Schindler
Hello,

sorted entries are still not available with Sysstat 11.4.3-2 in Debian Stretch.

:wq! PoC

PGP-Key: DDD3 4ABF 6413 38DE