| 30 |
|
#include <string.h> |
| 31 |
|
#include <time.h> |
| 32 |
|
#include "statgrab.h" |
| 33 |
+ |
#include "vector.h" |
| 34 |
|
|
| 35 |
|
#ifdef SOLARIS |
| 36 |
|
#include <sys/mnttab.h> |
| 62 |
|
#include <sys/ucred.h> |
| 63 |
|
#include <sys/mount.h> |
| 64 |
|
#endif |
| 65 |
< |
#ifdef FREEBSD |
| 65 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
| 66 |
|
#include <sys/dkstat.h> |
| 67 |
|
#include <devstat.h> |
| 68 |
|
#define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \ |
| 69 |
|
"ufs", "mfs"} |
| 70 |
|
#endif |
| 71 |
< |
#ifdef NETBSD |
| 71 |
> |
#if defined(NETBSD) || defined(OPENBSD) |
| 72 |
|
#include <sys/param.h> |
| 73 |
|
#include <sys/sysctl.h> |
| 74 |
|
#include <sys/disk.h> |
| 76 |
|
"ntfs"} |
| 77 |
|
#endif |
| 78 |
|
|
| 78 |
– |
#define START_VAL 1 |
| 79 |
– |
|
| 79 |
|
char *copy_string(char *orig_ptr, const char *newtext){ |
| 80 |
|
|
| 81 |
|
/* Maybe free if not NULL, and strdup rather than realloc and strcpy? */ |
| 88 |
|
return orig_ptr; |
| 89 |
|
} |
| 90 |
|
|
| 91 |
+ |
static void disk_stat_init(disk_stat_t *d) { |
| 92 |
+ |
d->device_name = NULL; |
| 93 |
+ |
d->fs_type = NULL; |
| 94 |
+ |
d->mnt_point = NULL; |
| 95 |
+ |
} |
| 96 |
|
|
| 97 |
< |
void init_disk_stat(int start, int end, disk_stat_t *disk_stats){ |
| 98 |
< |
|
| 99 |
< |
for(disk_stats+=start; start<=end; start++){ |
| 100 |
< |
disk_stats->device_name=NULL; |
| 97 |
< |
disk_stats->fs_type=NULL; |
| 98 |
< |
disk_stats->mnt_point=NULL; |
| 99 |
< |
|
| 100 |
< |
disk_stats++; |
| 101 |
< |
} |
| 97 |
> |
static void disk_stat_destroy(disk_stat_t *d) { |
| 98 |
> |
free(d->device_name); |
| 99 |
> |
free(d->fs_type); |
| 100 |
> |
free(d->mnt_point); |
| 101 |
|
} |
| 102 |
|
|
| 103 |
|
int is_valid_fs_type(const char *type) { |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
disk_stat_t *get_disk_stats(int *entries){ |
| 116 |
+ |
VECTOR_DECLARE_STATIC(disk_stats, disk_stat_t, 10, |
| 117 |
+ |
disk_stat_init, disk_stat_destroy); |
| 118 |
|
|
| 118 |
– |
static disk_stat_t *disk_stats; |
| 119 |
– |
static int watermark=-1; |
| 120 |
– |
|
| 119 |
|
int valid_type; |
| 120 |
|
int num_disks=0; |
| 121 |
|
#if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) |
| 137 |
|
struct statfs *mp; |
| 138 |
|
#endif |
| 139 |
|
|
| 142 |
– |
if(watermark==-1){ |
| 143 |
– |
disk_stats=malloc(START_VAL * sizeof(disk_stat_t)); |
| 144 |
– |
if(disk_stats==NULL){ |
| 145 |
– |
return NULL; |
| 146 |
– |
} |
| 147 |
– |
watermark=START_VAL; |
| 148 |
– |
init_disk_stat(0, watermark-1, disk_stats); |
| 149 |
– |
} |
| 140 |
|
#ifdef ALLBSD |
| 141 |
|
nummnt=getmntinfo(&mp , MNT_LOCAL); |
| 142 |
|
if (nummnt<=0){ |
| 171 |
|
#endif |
| 172 |
|
|
| 173 |
|
if(valid_type){ |
| 174 |
< |
if(num_disks>watermark-1){ |
| 175 |
< |
disk_ptr=disk_stats; |
| 186 |
< |
if((disk_stats=realloc(disk_stats, (watermark*2 * sizeof(disk_stat_t))))==NULL){ |
| 187 |
< |
disk_stats=disk_ptr; |
| 188 |
< |
return NULL; |
| 189 |
< |
} |
| 190 |
< |
|
| 191 |
< |
watermark=watermark*2; |
| 192 |
< |
init_disk_stat(num_disks, watermark-1, disk_stats); |
| 174 |
> |
if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) { |
| 175 |
> |
return NULL; |
| 176 |
|
} |
| 194 |
– |
|
| 177 |
|
disk_ptr=disk_stats+num_disks; |
| 178 |
+ |
|
| 179 |
|
#ifdef ALLBSD |
| 180 |
|
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){ |
| 181 |
|
return NULL; |
| 264 |
|
return disk_stats; |
| 265 |
|
|
| 266 |
|
} |
| 284 |
– |
void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){ |
| 267 |
|
|
| 268 |
< |
for(diskio_stats+=start; start<end; start++){ |
| 269 |
< |
diskio_stats->disk_name=NULL; |
| 288 |
< |
|
| 289 |
< |
diskio_stats++; |
| 290 |
< |
} |
| 268 |
> |
static void diskio_stat_init(diskio_stat_t *d) { |
| 269 |
> |
d->disk_name = NULL; |
| 270 |
|
} |
| 271 |
|
|
| 272 |
< |
diskio_stat_t *diskio_stat_malloc(int needed_entries, int *cur_entries, diskio_stat_t *diskio_stats){ |
| 273 |
< |
|
| 295 |
< |
if(diskio_stats==NULL){ |
| 296 |
< |
|
| 297 |
< |
if((diskio_stats=malloc(needed_entries * sizeof(diskio_stat_t)))==NULL){ |
| 298 |
< |
return NULL; |
| 299 |
< |
} |
| 300 |
< |
diskio_stat_init(0, needed_entries, diskio_stats); |
| 301 |
< |
*cur_entries=needed_entries; |
| 302 |
< |
|
| 303 |
< |
return diskio_stats; |
| 304 |
< |
} |
| 305 |
< |
|
| 306 |
< |
|
| 307 |
< |
if(*cur_entries<needed_entries){ |
| 308 |
< |
diskio_stats=realloc(diskio_stats, (sizeof(diskio_stat_t)*needed_entries)); |
| 309 |
< |
if(diskio_stats==NULL){ |
| 310 |
< |
return NULL; |
| 311 |
< |
} |
| 312 |
< |
diskio_stat_init(*cur_entries, needed_entries, diskio_stats); |
| 313 |
< |
*cur_entries=needed_entries; |
| 314 |
< |
} |
| 315 |
< |
|
| 316 |
< |
return diskio_stats; |
| 272 |
> |
static void diskio_stat_destroy(diskio_stat_t *d) { |
| 273 |
> |
free(d->disk_name); |
| 274 |
|
} |
| 275 |
|
|
| 276 |
< |
static diskio_stat_t *diskio_stats=NULL; |
| 277 |
< |
static int num_diskio=0; |
| 276 |
> |
VECTOR_DECLARE_STATIC(diskio_stats, diskio_stat_t, 10, |
| 277 |
> |
diskio_stat_init, diskio_stat_destroy); |
| 278 |
|
|
| 279 |
|
#ifdef LINUX |
| 280 |
|
typedef struct { |
| 284 |
|
#endif |
| 285 |
|
|
| 286 |
|
diskio_stat_t *get_diskio_stats(int *entries){ |
| 287 |
< |
|
| 331 |
< |
static int sizeof_diskio_stats=0; |
| 287 |
> |
int num_diskio; |
| 288 |
|
#ifndef LINUX |
| 289 |
|
diskio_stat_t *diskio_stats_ptr; |
| 290 |
|
#endif |
| 305 |
|
time_t now; |
| 306 |
|
const char *format; |
| 307 |
|
#endif |
| 308 |
< |
#ifdef FREEBSD |
| 308 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
| 309 |
|
static struct statinfo stats; |
| 310 |
|
static int stats_init = 0; |
| 311 |
|
int counter; |
| 316 |
|
#endif |
| 317 |
|
#ifdef NETBSD |
| 318 |
|
struct disk_sysctl *stats; |
| 319 |
+ |
#endif |
| 320 |
+ |
#ifdef OPENBSD |
| 321 |
+ |
int diskcount; |
| 322 |
+ |
char *disknames, *name, *bufpp; |
| 323 |
+ |
char **dk_name; |
| 324 |
+ |
struct diskstats *stats; |
| 325 |
+ |
#endif |
| 326 |
+ |
#ifdef NETBSD |
| 327 |
+ |
#define MIBSIZE 3 |
| 328 |
+ |
#endif |
| 329 |
+ |
#ifdef OPENBSD |
| 330 |
+ |
#define MIBSIZE 2 |
| 331 |
+ |
#endif |
| 332 |
+ |
#if defined(NETBSD) || defined(OPENBSD) |
| 333 |
|
int num_disks, i; |
| 334 |
< |
int mib[3]; |
| 334 |
> |
int mib[MIBSIZE]; |
| 335 |
|
size_t size; |
| 336 |
|
#endif |
| 337 |
|
|
| 338 |
|
num_diskio=0; |
| 339 |
|
|
| 340 |
< |
#ifdef NETBSD |
| 340 |
> |
#ifdef OPENBSD |
| 341 |
|
mib[0] = CTL_HW; |
| 342 |
+ |
mib[1] = HW_DISKCOUNT; |
| 343 |
+ |
|
| 344 |
+ |
size = sizeof(diskcount); |
| 345 |
+ |
if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) { |
| 346 |
+ |
return NULL; |
| 347 |
+ |
} |
| 348 |
+ |
|
| 349 |
+ |
mib[0] = CTL_HW; |
| 350 |
+ |
mib[1] = HW_DISKNAMES; |
| 351 |
+ |
|
| 352 |
+ |
if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) { |
| 353 |
+ |
return NULL; |
| 354 |
+ |
} |
| 355 |
+ |
|
| 356 |
+ |
disknames = malloc(size); |
| 357 |
+ |
if (disknames == NULL) { |
| 358 |
+ |
return NULL; |
| 359 |
+ |
} |
| 360 |
+ |
|
| 361 |
+ |
if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) { |
| 362 |
+ |
return NULL; |
| 363 |
+ |
} |
| 364 |
+ |
|
| 365 |
+ |
dk_name = calloc(diskcount, sizeof(char *)); |
| 366 |
+ |
bufpp = disknames; |
| 367 |
+ |
for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) { |
| 368 |
+ |
dk_name[i] = name; |
| 369 |
+ |
} |
| 370 |
+ |
#endif |
| 371 |
+ |
|
| 372 |
+ |
#if defined(NETBSD) || defined(OPENBSD) |
| 373 |
+ |
mib[0] = CTL_HW; |
| 374 |
|
mib[1] = HW_DISKSTATS; |
| 375 |
+ |
#ifdef NETBSD |
| 376 |
|
mib[2] = sizeof(struct disk_sysctl); |
| 377 |
+ |
#endif |
| 378 |
|
|
| 379 |
< |
if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) { |
| 379 |
> |
if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) { |
| 380 |
|
return NULL; |
| 381 |
|
} |
| 382 |
+ |
|
| 383 |
+ |
#ifdef NETBSD |
| 384 |
|
num_disks = size / sizeof(struct disk_sysctl); |
| 385 |
+ |
#else |
| 386 |
+ |
num_disks = size / sizeof(struct diskstats); |
| 387 |
+ |
#endif |
| 388 |
|
|
| 389 |
|
stats = malloc(size); |
| 390 |
|
if (stats == NULL) { |
| 391 |
|
return NULL; |
| 392 |
|
} |
| 393 |
|
|
| 394 |
< |
if (sysctl(mib, 3, stats, &size, NULL, 0) < 0) { |
| 394 |
> |
if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) { |
| 395 |
|
return NULL; |
| 396 |
|
} |
| 397 |
|
|
| 398 |
|
for (i = 0; i < num_disks; i++) { |
| 399 |
|
u_int64_t rbytes, wbytes; |
| 400 |
|
|
| 401 |
+ |
#ifdef NETBSD |
| 402 |
|
#ifdef HAVE_DK_RBYTES |
| 403 |
|
rbytes = stats[i].dk_rbytes; |
| 404 |
|
wbytes = stats[i].dk_wbytes; |
| 406 |
|
/* Before 1.7, NetBSD merged reads and writes. */ |
| 407 |
|
rbytes = wbytes = stats[i].dk_bytes; |
| 408 |
|
#endif |
| 409 |
+ |
#else |
| 410 |
+ |
rbytes = wbytes = stats[i].ds_bytes; |
| 411 |
+ |
#endif |
| 412 |
|
|
| 413 |
|
/* Don't keep stats for disks that have never been used. */ |
| 414 |
|
if (rbytes == 0 && wbytes == 0) { |
| 415 |
|
continue; |
| 416 |
|
} |
| 417 |
|
|
| 418 |
< |
diskio_stats = diskio_stat_malloc(num_diskio + 1, |
| 406 |
< |
&sizeof_diskio_stats, |
| 407 |
< |
diskio_stats); |
| 408 |
< |
if (diskio_stats == NULL) { |
| 418 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
| 419 |
|
return NULL; |
| 420 |
|
} |
| 421 |
|
diskio_stats_ptr = diskio_stats + num_diskio; |
| 425 |
|
if (diskio_stats_ptr->disk_name != NULL) { |
| 426 |
|
free(diskio_stats_ptr->disk_name); |
| 427 |
|
} |
| 428 |
+ |
#ifdef NETBSD |
| 429 |
|
diskio_stats_ptr->disk_name = strdup(stats[i].dk_name); |
| 430 |
+ |
#else |
| 431 |
+ |
diskio_stats_ptr->disk_name = strdup(dk_name[i]); |
| 432 |
+ |
#endif |
| 433 |
|
diskio_stats_ptr->systime = time(NULL); |
| 434 |
|
|
| 435 |
|
num_diskio++; |
| 436 |
|
} |
| 437 |
|
|
| 438 |
|
free(stats); |
| 439 |
+ |
#ifdef OPENBSD |
| 440 |
+ |
free(dk_name); |
| 441 |
+ |
free(disknames); |
| 442 |
|
#endif |
| 443 |
+ |
#endif |
| 444 |
|
|
| 445 |
< |
#ifdef FREEBSD |
| 445 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
| 446 |
|
if (!stats_init) { |
| 447 |
|
stats.dinfo=malloc(sizeof(struct devinfo)); |
| 448 |
|
if(stats.dinfo==NULL) return NULL; |
| 477 |
|
#else |
| 478 |
|
if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue; |
| 479 |
|
#endif |
| 480 |
< |
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
| 480 |
> |
|
| 481 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
| 482 |
|
return NULL; |
| 483 |
|
} |
| 484 |
|
diskio_stats_ptr=diskio_stats+num_diskio; |
| 513 |
|
if((kstat_read(kc, ksp, &kios))==-1){ |
| 514 |
|
} |
| 515 |
|
|
| 516 |
< |
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
| 516 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
| 517 |
|
kstat_close(kc); |
| 518 |
|
return NULL; |
| 519 |
|
} |
| 544 |
|
the same format. */ |
| 545 |
|
|
| 546 |
|
f = fopen("/proc/diskstats", "r"); |
| 547 |
< |
format = " %d %d %19s %*d %*d %lld %*d %*d %*d %lld"; |
| 547 |
> |
format = " %d %d %99s %*d %*d %lld %*d %*d %*d %lld"; |
| 548 |
|
if (f == NULL) { |
| 549 |
|
f = fopen("/proc/partitions", "r"); |
| 550 |
< |
format = " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld"; |
| 550 |
> |
format = " %d %d %*d %99s %*d %*d %lld %*d %*d %*d %lld"; |
| 551 |
|
} |
| 552 |
|
if (f == NULL) goto out; |
| 553 |
|
now = time(NULL); |
| 554 |
|
|
| 555 |
|
while ((line_ptr = f_read_line(f, "")) != NULL) { |
| 556 |
< |
char name[20]; |
| 556 |
> |
char name[100]; |
| 557 |
|
char *s; |
| 558 |
|
long long rsect, wsect; |
| 559 |
|
|
| 574 |
|
wsect = 0; |
| 575 |
|
} |
| 576 |
|
|
| 577 |
< |
diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats, |
| 578 |
< |
diskio_stats); |
| 579 |
< |
if (diskio_stats == NULL) goto out; |
| 577 |
> |
if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) { |
| 578 |
> |
goto out; |
| 579 |
> |
} |
| 580 |
|
if (n >= alloc_parts) { |
| 581 |
|
alloc_parts += 16; |
| 582 |
|
parts = realloc(parts, alloc_parts * sizeof *parts); |
| 683 |
|
} |
| 684 |
|
|
| 685 |
|
diskio_stat_t *get_diskio_stats_diff(int *entries){ |
| 686 |
< |
static diskio_stat_t *diff = NULL; |
| 687 |
< |
static int diff_count = 0; |
| 688 |
< |
diskio_stat_t *src, *dest; |
| 689 |
< |
int i, j, new_count; |
| 686 |
> |
VECTOR_DECLARE_STATIC(diff, diskio_stat_t, 1, |
| 687 |
> |
diskio_stat_init, diskio_stat_destroy); |
| 688 |
> |
diskio_stat_t *src = NULL, *dest; |
| 689 |
> |
int i, j, diff_count, new_count; |
| 690 |
|
|
| 691 |
|
if (diskio_stats == NULL) { |
| 692 |
|
/* No previous stats, so we can't calculate a difference. */ |
| 694 |
|
} |
| 695 |
|
|
| 696 |
|
/* Resize the results array to match the previous stats. */ |
| 697 |
< |
diff = diskio_stat_malloc(num_diskio, &diff_count, diff); |
| 698 |
< |
if (diff == NULL) { |
| 697 |
> |
diff_count = VECTOR_SIZE(diskio_stats); |
| 698 |
> |
if (VECTOR_RESIZE(diff, diff_count) < 0) { |
| 699 |
|
return NULL; |
| 700 |
|
} |
| 701 |
|
|