ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/disk_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/disk_stats.c (file contents):
Revision 1.19 by ats, Thu Aug 28 21:33:42 2003 UTC vs.
Revision 1.27 by pajs, Thu Oct 9 14:59:52 2003 UTC

# Line 296 | Line 296 | diskio_stat_t *diskio_stat_malloc(int needed_entries,
296   static diskio_stat_t *diskio_stats=NULL;        
297   static int num_diskio=0;        
298  
299 + #ifdef LINUX
300 + typedef struct {
301 +        int major;
302 +        int minor;
303 + } partition;
304 + #endif
305 +
306   diskio_stat_t *get_diskio_stats(int *entries){
307  
308          static int sizeof_diskio_stats=0;
# Line 311 | Line 318 | diskio_stat_t *get_diskio_stats(int *entries){
318          char *line_ptr;
319          int major, minor;
320          char dev_letter;
321 +        int has_pp_stats = 1;
322 +        static partition *parts = NULL;
323 +        static int alloc_parts = 0;
324 +        int i, n;
325 +        time_t now;
326   #endif
327   #ifdef FREEBSD
328 <        struct statinfo stats;
328 >        static struct statinfo stats;
329 >        static int stats_init = 0;
330          int counter;
331          struct device_selection *dev_sel = NULL;
332          int n_selected, n_selections;
# Line 323 | Line 336 | diskio_stat_t *get_diskio_stats(int *entries){
336          num_diskio=0;
337  
338   #ifdef FREEBSD
339 <        stats.dinfo=malloc(sizeof(struct devinfo));
340 <        if(stats.dinfo==NULL) return NULL;
339 >        if (!stats_init) {
340 >                stats.dinfo=malloc(sizeof(struct devinfo));
341 >                bzero(stats.dinfo, sizeof(struct devinfo));
342 >                if(stats.dinfo==NULL) return NULL;
343 >                stats_init = 1;
344 >        }
345          if ((getdevs(&stats)) < 0) return NULL;
346          /* Not aware of a get all devices, so i said 999. If we ever
347           * find a machine with more than 999 disks, then i'll change
348           * this number :)
349           */
350 + #ifdef FREEBSD5
351 +        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
352 + #else
353          if (selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) return NULL;
354 + #endif
355  
356          for(counter=0;counter<stats.dinfo->numdevs;counter++){
357                  dev_ptr=&stats.dinfo->devices[dev_sel[counter].position];
# Line 339 | Line 360 | diskio_stat_t *get_diskio_stats(int *entries){
360                   * devices.. like mem, proc.. and also doesn't report floppy
361                   * drives etc unless they are doing stuff :)
362                   */
363 + #ifdef FREEBSD5
364 +                if((dev_ptr->bytes[DEVSTAT_READ]==0) && (dev_ptr->bytes[DEVSTAT_WRITE]==0)) continue;
365 + #else
366                  if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
367 + #endif
368                  if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
369                          return NULL;
370                  }
371                  diskio_stats_ptr=diskio_stats+num_diskio;
372 <                
372 >
373 > #ifdef FREEBSD5        
374 >                diskio_stats_ptr->read_bytes=dev_ptr->bytes[DEVSTAT_READ];
375 >                diskio_stats_ptr->write_bytes=dev_ptr->bytes[DEVSTAT_WRITE];
376 > #else
377                  diskio_stats_ptr->read_bytes=dev_ptr->bytes_read;
378                  diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
379 + #endif
380                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
381                  asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
382                  diskio_stats_ptr->systime=time(NULL);
# Line 354 | Line 384 | diskio_stat_t *get_diskio_stats(int *entries){
384                  num_diskio++;
385          }
386          free(dev_sel);
357        free(stats.dinfo);
387  
388   #endif
389   #ifdef SOLARIS
# Line 393 | Line 422 | diskio_stat_t *get_diskio_stats(int *entries){
422   #endif
423  
424   #ifdef LINUX
425 <        f=fopen("/proc/stat", "r");
426 <        if(f==NULL){
427 <                *entries=0;
428 <                return NULL;
429 <        }
430 <        if((line_ptr=f_read_line(f, "disk_io:"))==NULL){
431 <                *entries=0;
432 <                fclose(f);
433 <                return NULL;
434 <        }
435 <        while((line_ptr=strchr(line_ptr, ' '))!=NULL){
436 <                line_ptr++;
437 <                if(*line_ptr=='\0'){
438 <                        break;
425 >        num_diskio = 0;
426 >        n = 0;
427 >
428 >        /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
429 >           have statistics in here too, so we can use those directly. */
430 >
431 >        f = fopen("/proc/partitions", "r");
432 >        if (f == NULL) goto out;
433 >        now = time(NULL);
434 >
435 >        while ((line_ptr = f_read_line(f, "")) != NULL) {
436 >                char name[20];
437 >                char *s;
438 >                long long rsect, wsect;
439 >
440 >                int nr = sscanf(line_ptr,
441 >                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
442 >                        &major, &minor, name, &rsect, &wsect);
443 >                if (nr < 3) continue;
444 >                if (nr < 5) {
445 >                        has_pp_stats = 0;
446 >                        rsect = 0;
447 >                        wsect = 0;
448                  }
411                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
412                        fclose(f);
413                        *entries=0;
414                        return NULL;
415                }
416                diskio_stats_ptr=diskio_stats+num_diskio;
449  
450 +                /* Skip device names ending in numbers, since they're
451 +                   partitions. */
452 +                s = name;
453 +                while (*s != '\0') s++;
454 +                --s;
455 +                if (*s >= '0' && *s <= '9') continue;
456  
457 <                if((sscanf(line_ptr, "(%d,%d):(%*d, %*d, %lld, %*d, %lld)", \
458 <                        &major, \
459 <                        &minor, \
460 <                        &diskio_stats_ptr->read_bytes, \
461 <                        &diskio_stats_ptr->write_bytes))!=4) {
462 <                                continue;
457 >                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
458 >                        diskio_stats);
459 >                if (diskio_stats == NULL) goto out;
460 >                if (n >= alloc_parts) {
461 >                        alloc_parts += 16;
462 >                        parts = realloc(parts, alloc_parts * sizeof *parts);
463 >                        if (parts == NULL) {
464 >                                alloc_parts = 0;
465 >                                goto out;
466 >                        }
467                  }
468  
469 <                /* We read the number of blocks. Blocks are stored in 512 bytes */
470 <                diskio_stats_ptr->read_bytes=diskio_stats_ptr->read_bytes*512;
471 <                diskio_stats_ptr->write_bytes=diskio_stats_ptr->write_bytes*512;
469 >                if (diskio_stats[n].disk_name != NULL)
470 >                        free(diskio_stats[n].disk_name);
471 >                diskio_stats[n].disk_name = strdup(name);
472 >                diskio_stats[n].read_bytes = rsect * 512;
473 >                diskio_stats[n].write_bytes = wsect * 512;
474 >                diskio_stats[n].systime = now;
475 >                parts[n].major = major;
476 >                parts[n].minor = minor;
477  
478 <                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
478 >                n++;
479 >        }
480  
481 <                switch(major){
482 <                        case 2:
483 <                                if(minor==0){
436 <                                        diskio_stats_ptr->disk_name=strdup("fd0");
437 <                                }
438 <                                break;
481 >        if (!has_pp_stats) {
482 >                /* This is an older kernel without stats in /proc/partitions.
483 >                   Read what we can from /proc/stat instead. */
484  
485 <                        case 3:
486 <                                if(minor==0){
487 <                                        diskio_stats_ptr->disk_name=strdup("hda");
488 <                                }else{
489 <                                        diskio_stats_ptr->disk_name=strdup("hdb");
490 <                                }
491 <                                break;
485 >                f = fopen("/proc/stat", "r");
486 >                if (f == NULL) goto out;
487 >                now = time(NULL);
488 >        
489 >                line_ptr = f_read_line(f, "disk_io:");
490 >                if (line_ptr == NULL) goto out;
491 >        
492 >                while((line_ptr=strchr(line_ptr, ' '))!=NULL){
493 >                        long long rsect, wsect;
494  
495 +                        if (*++line_ptr == '\0') break;
496 +        
497 +                        if((sscanf(line_ptr,
498 +                                "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
499 +                                &major, &minor, &rsect, &wsect)) != 4) {
500 +                                        continue;
501 +                        }
502 +
503 +                        /* Find the corresponding device from earlier.
504 +                           Just to add to the fun, "minor" is actually the disk
505 +                           number, not the device minor, so we need to figure
506 +                           out the real minor number based on the major!
507 +                           This list is not exhaustive; if you're running
508 +                           an older kernel you probably don't have fancy
509 +                           I2O hardware anyway... */
510 +                        switch (major) {
511 +                        case 3:
512 +                        case 21:
513                          case 22:
514 <                                if(minor==0){
515 <                                        diskio_stats_ptr->disk_name=strdup("hdc");
516 <                                }else{
517 <                                        diskio_stats_ptr->disk_name=strdup("hdd");
518 <                                }
514 >                        case 33:
515 >                        case 34:
516 >                        case 36:
517 >                        case 56:
518 >                        case 57:
519 >                        case 88:
520 >                        case 89:
521 >                        case 90:
522 >                        case 91:
523 >                                minor *= 64;
524                                  break;
525 <                        case 8:
526 <                                dev_letter='a'+(minor/16);
457 <                                diskio_stats_ptr->disk_name=malloc(4);
458 <                                snprintf(diskio_stats_ptr->disk_name, 4, "sd%c", dev_letter);
525 >                        case 9:
526 >                        case 43:
527                                  break;
528                          default:
529 <                                /* I have no idea what it is then :) */
462 <                                diskio_stats_ptr->disk_name=malloc(16);
463 <                                snprintf(diskio_stats_ptr->disk_name, 16, "%d %d", major, minor);
529 >                                minor *= 16;
530                                  break;
531 <                }
531 >                        }
532 >                        for (i = 0; i < n; i++) {
533 >                                if (major == parts[i].major
534 >                                        && minor == parts[i].minor)
535 >                                        break;
536 >                        }
537 >                        if (i == n) continue;
538  
539 <                diskio_stats_ptr->systime=time(NULL);
540 <                num_diskio++;
539 >                        /* We read the number of blocks. Blocks are stored in
540 >                           512 bytes */
541 >                        diskio_stats[i].read_bytes = rsect * 512;
542 >                        diskio_stats[i].write_bytes = wsect * 512;
543 >                        diskio_stats[i].systime = now;
544 >                }
545          }
546  
547 <        fclose(f);
547 >        num_diskio = n;
548 > out:
549 >        if (f != NULL) fclose(f);
550  
551   #endif
552          *entries=num_diskio;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines