/usr.bin/systat/pigs.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 193 lines · 136 code · 23 blank · 34 comment · 25 complexity · 94c567b408d24a75d0e5697aec2c7d41 MD5 · raw file

  1. /*-
  2. * Copyright (c) 1980, 1992, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #if 0
  30. #ifndef lint
  31. static char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93";
  32. #endif /* not lint */
  33. #endif
  34. #include <sys/cdefs.h>
  35. __FBSDID("$FreeBSD$");
  36. /*
  37. * Pigs display from Bill Reeves at Lucasfilm
  38. */
  39. #include <sys/param.h>
  40. #include <sys/proc.h>
  41. #include <sys/sysctl.h>
  42. #include <sys/time.h>
  43. #include <sys/user.h>
  44. #include <curses.h>
  45. #include <math.h>
  46. #include <pwd.h>
  47. #include <stdlib.h>
  48. #include "systat.h"
  49. #include "extern.h"
  50. int compar(const void *, const void *);
  51. static int nproc;
  52. static struct p_times {
  53. float pt_pctcpu;
  54. struct kinfo_proc *pt_kp;
  55. } *pt;
  56. static int fscale;
  57. static double lccpu;
  58. WINDOW *
  59. openpigs(void)
  60. {
  61. return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
  62. }
  63. void
  64. closepigs(WINDOW *w)
  65. {
  66. if (w == NULL)
  67. return;
  68. wclear(w);
  69. wrefresh(w);
  70. delwin(w);
  71. }
  72. void
  73. showpigs(void)
  74. {
  75. int i, j, y, k;
  76. const char *uname, *pname;
  77. char pidname[30];
  78. if (pt == NULL)
  79. return;
  80. qsort(pt, nproc, sizeof (struct p_times), compar);
  81. y = 1;
  82. i = nproc;
  83. if (i > wnd->_maxy-1)
  84. i = wnd->_maxy-1;
  85. for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
  86. uname = user_from_uid(pt[k].pt_kp->ki_uid, 0);
  87. pname = pt[k].pt_kp->ki_comm;
  88. wmove(wnd, y, 0);
  89. wclrtoeol(wnd);
  90. mvwaddstr(wnd, y, 0, uname);
  91. snprintf(pidname, sizeof(pidname), "%10.10s", pname);
  92. mvwaddstr(wnd, y, 9, pidname);
  93. wmove(wnd, y, 20);
  94. for (j = pt[k].pt_pctcpu * 50 + 0.5; j > 0; j--)
  95. waddch(wnd, 'X');
  96. }
  97. wmove(wnd, y, 0); wclrtobot(wnd);
  98. }
  99. int
  100. initpigs(void)
  101. {
  102. fixpt_t ccpu;
  103. size_t len;
  104. int err;
  105. len = sizeof(ccpu);
  106. err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0);
  107. if (err || len != sizeof(ccpu)) {
  108. perror("kern.ccpu");
  109. return (0);
  110. }
  111. len = sizeof(fscale);
  112. err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0);
  113. if (err || len != sizeof(fscale)) {
  114. perror("kern.fscale");
  115. return (0);
  116. }
  117. lccpu = log((double) ccpu / fscale);
  118. return(1);
  119. }
  120. void
  121. fetchpigs(void)
  122. {
  123. int i;
  124. float ftime;
  125. float *pctp;
  126. struct kinfo_proc *kpp;
  127. static int lastnproc = 0;
  128. if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
  129. error("%s", kvm_geterr(kd));
  130. if (pt)
  131. free(pt);
  132. return;
  133. }
  134. if (nproc > lastnproc) {
  135. free(pt);
  136. if ((pt =
  137. malloc(nproc * sizeof(struct p_times))) == NULL) {
  138. error("Out of memory");
  139. die(0);
  140. }
  141. }
  142. lastnproc = nproc;
  143. /*
  144. * calculate %cpu for each proc
  145. */
  146. for (i = 0; i < nproc; i++) {
  147. pt[i].pt_kp = &kpp[i];
  148. pctp = &pt[i].pt_pctcpu;
  149. ftime = kpp[i].ki_swtime;
  150. if (ftime == 0 || (kpp[i].ki_flag & P_INMEM) == 0)
  151. *pctp = 0;
  152. else
  153. *pctp = ((double) kpp[i].ki_pctcpu /
  154. fscale) / (1.0 - exp(ftime * lccpu));
  155. }
  156. }
  157. void
  158. labelpigs(void)
  159. {
  160. wmove(wnd, 0, 0);
  161. wclrtoeol(wnd);
  162. mvwaddstr(wnd, 0, 20,
  163. "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
  164. }
  165. int
  166. compar(const void *a, const void *b)
  167. {
  168. return (((const struct p_times *) a)->pt_pctcpu >
  169. ((const struct p_times *) b)->pt_pctcpu)? -1: 1;
  170. }