comparison src/getloadavg.c @ 278:90d73dddcdc4 r21-0b37

Import from CVS: tag r21-0b37
author cvs
date Mon, 13 Aug 2007 10:31:29 +0200
parents c5d627a313b1
children 7df0dd720c89
comparison
equal deleted inserted replaced
277:cfdf3ff11843 278:90d73dddcdc4
501 501
502 #ifdef SUNOS_5 502 #ifdef SUNOS_5
503 static kvm_t *kd; 503 static kvm_t *kd;
504 #endif /* SUNOS_5 */ 504 #endif /* SUNOS_5 */
505 505
506 #ifndef countof
507 # define countof(x) (sizeof (x) / sizeof (*(x)))
508 #endif
509
506 #endif /* LOAD_AVE_TYPE */ 510 #endif /* LOAD_AVE_TYPE */
507 511
508 /* Put the 1 minute, 5 minute and 15 minute load averages 512 /* Put the 1 minute, 5 minute and 15 minute load averages
509 into the first NELEM elements of LOADAVG. 513 into the first NELEM elements of LOADAVG.
510 Return the number written (never more than 3, but may be less than NELEM), 514 Return the number written (never more than 3, but may be less than NELEM),
518 #ifdef NO_GET_LOAD_AVG 522 #ifdef NO_GET_LOAD_AVG
519 #define LDAV_DONE 523 #define LDAV_DONE
520 /* Set errno to zero to indicate that there was no particular error; 524 /* Set errno to zero to indicate that there was no particular error;
521 this function just can't work at all on this system. */ 525 this function just can't work at all on this system. */
522 errno = 0; 526 errno = 0;
523 elem = -1; 527 elem = -2;
524 #endif 528 #endif
525 529
526 #ifdef XEMACS
527 #if ! defined (LDAV_DONE) && defined (HAVE_KSTAT_H) && defined (HAVE_LIBKSTAT) 530 #if ! defined (LDAV_DONE) && defined (HAVE_KSTAT_H) && defined (HAVE_LIBKSTAT)
528 #define LDAV_DONE 531 #define LDAV_DONE
529 532 /* getloadavg is best implemented using kstat (kernel stats), on
530 /* getloadavg is best implemented using kstat (kernel stats), 533 systems (like SunOS5) that support it, since you don't need special
531 on systems (like SunOS5) that support it, 534 privileges to use it.
532 since you don't have to be superusers to use it. 535
533 Thanks to Zlatko Calusic <zcalusic@srce.hr>. 536 Initial implementation courtesy Zlatko Calusic <zcalusic@carnet.hr>.
534 Integrated to XEmacs by Hrvoje Niksic <hniksic@srce.hr>. */ 537 Integrated to XEmacs by Hrvoje Niksic <hniksic@srce.hr>.
535 static kstat_ctl_t *kc; 538 Additional cleanup by Hrvoje Niksic, based on code published by
536 static kstat_t *ksp; 539 Casper Dik <Casper.Dik@Holland.Sun.Com>. */
537 static kstat_named_t *buf; 540 kstat_ctl_t *kc;
538 541 kstat_t *ksp;
539 if (!getloadavg_initialized) 542 static char *avestrings[] = { "avenrun_1min",
540 { 543 "avenrun_5min",
541 kc = kstat_open(); 544 "avenrun_15min" };
542 if (!kc) 545
543 return -1; 546 if (nelem > countof (avestrings))
544 getloadavg_initialized = 1; 547 nelem = countof (avestrings);
545 } 548
546 ksp = kstat_lookup(kc, "unix", 0, "system_misc"); 549 kc = kstat_open();
550 if (!kc)
551 return -1;
552 ksp = kstat_lookup (kc, "unix", 0, "system_misc");
547 if (!ksp) 553 if (!ksp)
554 {
555 kstat_close (kc);
548 return -1; 556 return -1;
549 if (kstat_read(kc, ksp, ksp->ks_data) < 0) 557 }
550 return -1; 558 if (kstat_read (kc, ksp, 0) < 0)
551 buf = (kstat_named_t *) malloc (ksp->ks_data_size); 559 {
552 if (!buf) 560 kstat_close (kc);
553 return -1; 561 return -1;
554 memcpy(buf, ksp->ks_data, ksp->ks_data_size); 562 }
555 if (nelem > 3)
556 nelem = 3;
557 for (elem = 0; elem < nelem; elem++) 563 for (elem = 0; elem < nelem; elem++)
558 loadavg[elem] = (buf + 6 + elem)->value.ul / 256.0; 564 {
559 free(buf); 565 kstat_named_t *kn = kstat_data_lookup (ksp, avestrings[elem]);
560 566 if (!kn)
567 {
568 kstat_close (kc);
569 return -1;
570 }
571 loadavg[elem] = (double)kn->value.ul/FSCALE;
572 }
573 kstat_close (kc);
561 #endif /* HAVE_KSTAT_H && HAVE_LIBKSTAT */ 574 #endif /* HAVE_KSTAT_H && HAVE_LIBKSTAT */
562 575
563 #if !defined (LDAV_DONE) && defined (HAVE_SYS_PSTAT_H) 576 #if !defined (LDAV_DONE) && defined (HAVE_SYS_PSTAT_H)
564 #define LDAV_DONE 577 #define LDAV_DONE
565 /* This is totally undocumented, and is not guaranteed to work, but 578 /* This is totally undocumented, and is not guaranteed to work, but
577 loadavg[elem++] = procinfo.psd_avg_1_min; 590 loadavg[elem++] = procinfo.psd_avg_1_min;
578 loadavg[elem++] = procinfo.psd_avg_5_min; 591 loadavg[elem++] = procinfo.psd_avg_5_min;
579 loadavg[elem++] = procinfo.psd_avg_15_min; 592 loadavg[elem++] = procinfo.psd_avg_15_min;
580 #endif /* HPUX */ 593 #endif /* HPUX */
581 594
582 #endif /* XEMACS */
583
584 #if !defined (LDAV_DONE) && defined (__linux__) 595 #if !defined (LDAV_DONE) && defined (__linux__)
585 #define LDAV_DONE 596 #define LDAV_DONE
586 #undef LOAD_AVE_TYPE 597 #undef LOAD_AVE_TYPE
587 598
588 #ifndef LINUX_LDAV_FILE 599 #ifndef LINUX_LDAV_FILE
606 if (count < 1) 617 if (count < 1)
607 return -1; 618 return -1;
608 619
609 for (elem = 0; elem < nelem && elem < count; elem++) 620 for (elem = 0; elem < nelem && elem < count; elem++)
610 loadavg[elem] = load_ave[elem]; 621 loadavg[elem] = load_ave[elem];
611
612 return elem;
613
614 #endif /* __linux__ */ 622 #endif /* __linux__ */
615 623
616 #if !defined (LDAV_DONE) && defined (__NetBSD__) 624 #if !defined (LDAV_DONE) && defined (__NetBSD__)
617 #define LDAV_DONE 625 #define LDAV_DONE
618 #undef LOAD_AVE_TYPE 626 #undef LOAD_AVE_TYPE
635 if (count != 4) 643 if (count != 4)
636 return -1; 644 return -1;
637 645
638 for (elem = 0; elem < nelem; elem++) 646 for (elem = 0; elem < nelem; elem++)
639 loadavg[elem] = (double) load_ave[elem] / (double) scale; 647 loadavg[elem] = (double) load_ave[elem] / (double) scale;
640
641 return elem;
642
643 #endif /* __NetBSD__ */ 648 #endif /* __NetBSD__ */
644 649
645 #if !defined (LDAV_DONE) && defined (NeXT) 650 #if !defined (LDAV_DONE) && defined (NeXT)
646 #define LDAV_DONE 651 #define LDAV_DONE
647 /* The NeXT code was adapted from iscreen 3.2. */ 652 /* The NeXT code was adapted from iscreen 3.2. */
942 return elem; 947 return elem;
943 #else 948 #else
944 /* Set errno to zero to indicate that there was no particular error; 949 /* Set errno to zero to indicate that there was no particular error;
945 this function just can't work at all on this system. */ 950 this function just can't work at all on this system. */
946 errno = 0; 951 errno = 0;
947 return -1; 952 return -2;
948 #endif 953 #endif
949 } 954 }
950 955
951 #endif /* ! HAVE_GETLOADAVG */ 956 #endif /* ! HAVE_GETLOADAVG */
952 957