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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines