1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
|
/*
* mpstat: per-processor statistics
* (C) 2000-2016 by Sebastien GODARD (sysstat <at> orange.fr)
*
***************************************************************************
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 2 of the License, or (at your *
* option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA *
***************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <ctype.h>
#include <sys/utsname.h>
#include "version.h"
#include "mpstat.h"
#include "common.h"
#include "rd_stats.h"
#include "count.h"
#ifdef USE_NLS
#include <locale.h>
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif
unsigned long long uptime[3] = {0, 0, 0};
unsigned long long uptime0[3] = {0, 0, 0};
/* NOTE: Use array of _char_ for bitmaps to avoid endianness problems...*/
unsigned char *cpu_bitmap; /* Bit 0: Global; Bit 1: 1st proc; etc. */
/* Structure used to save CPU stats */
struct stats_cpu *st_cpu[3];
/*
* Structure used to save total number of interrupts received
* among all CPU and for each CPU.
*/
struct stats_irq *st_irq[3];
/*
* Structures used to save, for each interrupt, the number
* received by each CPU.
*/
struct stats_irqcpu *st_irqcpu[3];
struct stats_irqcpu *st_softirqcpu[3];
struct tm mp_tstamp[3];
/* Activity flag */
unsigned int actflags = 0;
unsigned int flags = 0;
/* Interval and count parameters */
long interval = -1, count = 0;
/* Nb of processors on the machine */
int cpu_nr = 0;
/* Nb of interrupts per processor */
int irqcpu_nr = 0;
/* Nb of soft interrupts per processor */
int softirqcpu_nr = 0;
struct sigaction alrm_act, int_act;
int sigint_caught = 0;
/*
***************************************************************************
* Print usage and exit
*
* IN:
* @progname Name of sysstat command
***************************************************************************
*/
void usage(char *progname)
{
fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
progname);
fprintf(stderr, _("Options are:\n"
"[ -A ] [ -u ] [ -V ] [ -I { SUM | CPU | SCPU | ALL } ]\n"
"[ -P { <cpu> [,...] | ON | ALL } ]\n"));
exit(1);
}
/*
***************************************************************************
* SIGALRM signal handler. No need to reset the handler here.
*
* IN:
* @sig Signal number.
***************************************************************************
*/
void alarm_handler(int sig)
{
alarm(interval);
}
/*
***************************************************************************
* SIGINT signal handler.
*
* IN:
* @sig Signal number.
**************************************************************************
*/
void int_handler(int sig)
{
sigint_caught = 1;
}
/*
***************************************************************************
* Allocate stats structures and cpu bitmap.
*
* IN:
* @nr_cpus Number of CPUs. This is the real number of available CPUs + 1
* because we also have to allocate a structure for CPU 'all'.
***************************************************************************
*/
void salloc_mp_struct(int nr_cpus)
{
int i;
for (i = 0; i < 3; i++) {
if ((st_cpu[i] = (struct stats_cpu *) malloc(STATS_CPU_SIZE * nr_cpus))
== NULL) {
perror("malloc");
exit(4);
}
memset(st_cpu[i], 0, STATS_CPU_SIZE * nr_cpus);
if ((st_irq[i] = (struct stats_irq *) malloc(STATS_IRQ_SIZE * nr_cpus))
== NULL) {
perror("malloc");
exit(4);
}
memset(st_irq[i], 0, STATS_IRQ_SIZE * nr_cpus);
if ((st_irqcpu[i] = (struct stats_irqcpu *) malloc(STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr))
== NULL) {
perror("malloc");
exit(4);
}
memset(st_irqcpu[i], 0, STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr);
if ((st_softirqcpu[i] = (struct stats_irqcpu *) malloc(STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr))
== NULL) {
perror("malloc");
exit(4);
}
memset(st_softirqcpu[i], 0, STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr);
}
if ((cpu_bitmap = (unsigned char *) malloc((nr_cpus >> 3) + 1)) == NULL) {
perror("malloc");
exit(4);
}
memset(cpu_bitmap, 0, (nr_cpus >> 3) + 1);
}
/*
***************************************************************************
* Free structures and bitmap.
***************************************************************************
*/
void sfree_mp_struct(void)
{
int i;
for (i = 0; i < 3; i++) {
free(st_cpu[i]);
free(st_irq[i]);
free(st_irqcpu[i]);
free(st_softirqcpu[i]);
}
free(cpu_bitmap);
}
/*
***************************************************************************
* Display CPU statistics.
*
* IN:
* @dis TRUE if a header line must be printed.
* @g_itv Interval value in jiffies multiplied by the number of CPU.
* @prev Position in array where statistics used as reference are.
* Stats used as reference may be the previous ones read, or
* the very first ones when calculating the average.
* @curr Position in array where current statistics will be saved.
* @prev_string String displayed at the beginning of a header line. This is
* the timestamp of the previous sample, or "Average" when
* displaying average stats.
* @curr_string String displayed at the beginning of current sample stats.
* This is the timestamp of the current sample, or "Average"
* when displaying average stats.
***************************************************************************
*/
void write_cpu_stats(int dis, unsigned long long g_itv, int prev, int curr,
char *prev_string, char *curr_string)
{
struct stats_cpu *scc, *scp;
unsigned long long pc_itv;
int cpu;
if (dis) {
printf("\n%-11s CPU %%usr %%nice %%sys %%iowait %%irq "
"%%soft %%steal %%guest %%gnice %%idle\n",
prev_string);
}
/* Check if we want global stats among all proc */
if (*cpu_bitmap & 1) {
printf("%-11s", curr_string);
cprintf_in(IS_STR, " %s", " all", 0);
cprintf_pc(10, 7, 2,
(st_cpu[curr]->cpu_user - st_cpu[curr]->cpu_guest) <
(st_cpu[prev]->cpu_user - st_cpu[prev]->cpu_guest) ?
0.0 :
ll_sp_value(st_cpu[prev]->cpu_user - st_cpu[prev]->cpu_guest,
st_cpu[curr]->cpu_user - st_cpu[curr]->cpu_guest,
g_itv),
(st_cpu[curr]->cpu_nice - st_cpu[curr]->cpu_guest_nice) <
(st_cpu[prev]->cpu_nice - st_cpu[prev]->cpu_guest_nice) ?
0.0 :
ll_sp_value(st_cpu[prev]->cpu_nice - st_cpu[prev]->cpu_guest_nice,
st_cpu[curr]->cpu_nice - st_cpu[curr]->cpu_guest_nice,
g_itv),
ll_sp_value(st_cpu[prev]->cpu_sys,
st_cpu[curr]->cpu_sys,
g_itv),
ll_sp_value(st_cpu[prev]->cpu_iowait,
st_cpu[curr]->cpu_iowait,
g_itv),
ll_sp_value(st_cpu[prev]->cpu_hardirq,
st_cpu[curr]->cpu_hardirq,
g_itv),
ll_sp_value(st_cpu[prev]->cpu_softirq,
st_cpu[curr]->cpu_softirq,
g_itv),
ll_sp_value(st_cpu[prev]->cpu_steal,
st_cpu[curr]->cpu_steal,
g_itv),
ll_sp_value(st_cpu[prev]->cpu_guest,
st_cpu[curr]->cpu_guest,
g_itv),
ll_sp_value(st_cpu[prev]->cpu_guest_nice,
st_cpu[curr]->cpu_guest_nice,
g_itv),
(st_cpu[curr]->cpu_idle < st_cpu[prev]->cpu_idle) ?
0.0 :
ll_sp_value(st_cpu[prev]->cpu_idle,
st_cpu[curr]->cpu_idle,
g_itv));
printf("\n");
}
for (cpu = 1; cpu <= cpu_nr; cpu++) {
scc = st_cpu[curr] + cpu;
scp = st_cpu[prev] + cpu;
/* Check if we want stats about this proc */
if (!(*(cpu_bitmap + (cpu >> 3)) & (1 << (cpu & 0x07))))
continue;
/*
* If the CPU is offline then it is omited from /proc/stat
* and the sum of all values is zero.
* (Remember that guest/guest_nice times are already included in
* user/nice modes.)
*/
if ((scc->cpu_user + scc->cpu_nice + scc->cpu_sys +
scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal +
scc->cpu_hardirq + scc->cpu_softirq) == 0) {
if (!DISPLAY_ONLINE_CPU(flags)) {
printf("%-11s", curr_string);
cprintf_in(IS_INT, " %4d", "", cpu - 1);
cprintf_pc(10, 7, 2,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
printf("\n");
}
continue;
}
printf("%-11s", curr_string);
cprintf_in(IS_INT, " %4d", "", cpu - 1);
/* Recalculate itv for current proc */
pc_itv = get_per_cpu_interval(scc, scp);
if (!pc_itv) {
/*
* If the CPU is tickless then there is no change in CPU values
* but the sum of values is not zero.
*/
cprintf_pc(10, 7, 2,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0);
printf("\n");
}
else {
cprintf_pc(10, 7, 2,
(scc->cpu_user - scc->cpu_guest) < (scp->cpu_user - scp->cpu_guest) ?
0.0 :
ll_sp_value(scp->cpu_user - scp->cpu_guest,
scc->cpu_user - scc->cpu_guest,
pc_itv),
(scc->cpu_nice - scc->cpu_guest_nice) < (scp->cpu_nice - scp->cpu_guest_nice) ?
0.0 :
ll_sp_value(scp->cpu_nice - scp->cpu_guest_nice,
scc->cpu_nice - scc->cpu_guest_nice,
pc_itv),
ll_sp_value(scp->cpu_sys,
scc->cpu_sys,
pc_itv),
ll_sp_value(scp->cpu_iowait,
scc->cpu_iowait,
pc_itv),
ll_sp_value(scp->cpu_hardirq,
scc->cpu_hardirq,
pc_itv),
ll_sp_value(scp->cpu_softirq,
scc->cpu_softirq,
pc_itv),
ll_sp_value(scp->cpu_steal,
scc->cpu_steal,
pc_itv),
ll_sp_value(scp->cpu_guest,
scc->cpu_guest,
pc_itv),
ll_sp_value(scp->cpu_guest_nice,
scc->cpu_guest_nice,
pc_itv),
(scc->cpu_idle < scp->cpu_idle) ?
0.0 :
ll_sp_value(scp->cpu_idle,
scc->cpu_idle,
pc_itv));
printf("\n");
}
}
}
/*
***************************************************************************
* Display total number of interrupts per CPU.
*
* IN:
* @dis TRUE if a header line must be printed.
* @itv Interval value.
* @prev Position in array where statistics used as reference are.
* Stats used as reference may be the previous ones read, or
* the very first ones when calculating the average.
* @curr Position in array where current statistics will be saved.
* @prev_string String displayed at the beginning of a header line. This is
* the timestamp of the previous sample, or "Average" when
* displaying average stats.
* @curr_string String displayed at the beginning of current sample stats.
* This is the timestamp of the current sample, or "Average"
* when displaying average stats.
***************************************************************************
*/
void write_isumcpu_stats(int dis, unsigned long long itv, int prev, int curr,
char *prev_string, char *curr_string)
{
struct stats_cpu *scc, *scp;
struct stats_irq *sic, *sip;
unsigned long long pc_itv;
int cpu;
if (dis) {
printf("\n%-11s CPU intr/s\n", prev_string);
}
if (*cpu_bitmap & 1) {
printf("%-11s", curr_string);
cprintf_in(IS_STR, " %s", " all", 0);
/* Print total number of interrupts among all cpu */
cprintf_f(1, 9, 2,
S_VALUE(st_irq[prev]->irq_nr, st_irq[curr]->irq_nr, itv));
printf("\n");
}
for (cpu = 1; cpu <= cpu_nr; cpu++) {
sic = st_irq[curr] + cpu;
sip = st_irq[prev] + cpu;
scc = st_cpu[curr] + cpu;
scp = st_cpu[prev] + cpu;
/* Check if we want stats about this CPU */
if (!(*(cpu_bitmap + (cpu >> 3)) & (1 << (cpu & 0x07))))
continue;
if ((scc->cpu_user + scc->cpu_nice + scc->cpu_sys +
scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal +
scc->cpu_hardirq + scc->cpu_softirq) == 0) {
/* This is an offline CPU */
if (!DISPLAY_ONLINE_CPU(flags)) {
/*
* Display offline CPU if requested by the user.
* Value displayed is 0.00.
*/
printf("%-11s", curr_string);
cprintf_in(IS_INT, " %4d", "", cpu- 1);
cprintf_f(1, 9, 2, 0.0);
printf("\n");
}
continue;
}
printf("%-11s", curr_string);
cprintf_in(IS_INT, " %4d", "", cpu - 1);
/* Recalculate itv for current proc */
pc_itv = get_per_cpu_interval(scc, scp);
if (!pc_itv) {
/* This is a tickless CPU: Value displayed is 0.00 */
cprintf_f(1, 9, 2, 0.0);
printf("\n");
}
else {
/* Display total number of interrupts for current CPU */
cprintf_f(1, 9, 2,
S_VALUE(sip->irq_nr, sic->irq_nr, itv));
printf("\n");
}
}
}
/*
***************************************************************************
* Display interrupts statistics for each CPU.
*
* IN:
* @st_ic Array for per-CPU statistics.
* @ic_nr Number of interrupts (hard or soft) per CPU.
* @dis TRUE if a header line must be printed.
* @itv Interval value.
* @prev Position in array where statistics used as reference are.
* Stats used as reference may be the previous ones read, or
* the very first ones when calculating the average.
* @curr Position in array where current statistics will be saved.
* @prev_string String displayed at the beginning of a header line. This is
* the timestamp of the previous sample, or "Average" when
* displaying average stats.
* @curr_string String displayed at the beginning of current sample stats.
* This is the timestamp of the current sample, or "Average"
* when displaying average stats.
***************************************************************************
*/
void write_irqcpu_stats(struct stats_irqcpu *st_ic[], int ic_nr, int dis,
unsigned long long itv, int prev, int curr,
char *prev_string, char *curr_string)
{
struct stats_cpu *scc;
int j = ic_nr, offset, cpu, colwidth[NR_IRQS];
struct stats_irqcpu *p, *q, *p0, *q0;
/*
* Check if number of interrupts has changed.
* If this is the case, the header line will be printed again.
* NB: A zero interval value indicates that we are
* displaying statistics since system startup.
*/
if (!dis && interval) {
for (j = 0; j < ic_nr; j++) {
p0 = st_ic[curr] + j;
q0 = st_ic[prev] + j;
if (strcmp(p0->irq_name, q0->irq_name))
/*
* These are two different interrupts: The header must be displayed
* (maybe an interrupt has disappeared, or a new one has just been registered).
* Note that we compare even empty strings for the case where
* a disappearing interrupt would be the last one in the list.
*/
break;
}
}
if (dis || (j < ic_nr)) {
/* Print header */
printf("\n%-11s CPU", prev_string);
for (j = 0; j < ic_nr; j++) {
p0 = st_ic[curr] + j;
if (p0->irq_name[0] == '\0')
/* End of the list of interrupts */
break;
printf(" %8s/s", p0->irq_name);
}
printf("\n");
}
/* Calculate column widths */
for (j = 0; j < ic_nr; j++) {
p0 = st_ic[curr] + j;
/*
* Width is IRQ name + 2 for the trailing "/s".
* Width is calculated even for "undefined" interrupts (with
* an empty irq_name string) to quiet code analysis tools.
*/
colwidth[j] = strlen(p0->irq_name) + 2;
/*
* Normal space for printing a number is 11 chars
* (space + 10 digits including the period).
*/
if (colwidth[j] < 10) {
colwidth[j] = 10;
}
}
for (cpu = 1; cpu <= cpu_nr; cpu++) {
scc = st_cpu[curr] + cpu;
/*
* Check if we want stats about this CPU.
* CPU must have been explicitly selected using option -P,
* else we display every CPU.
*/
if (!(*(cpu_bitmap + (cpu >> 3)) & (1 << (cpu & 0x07))) && USE_P_OPTION(flags))
continue;
if ((scc->cpu_user + scc->cpu_nice + scc->cpu_sys +
scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal +
scc->cpu_hardirq + scc->cpu_softirq) == 0) {
/* Offline CPU found */
if (DISPLAY_ONLINE_CPU(flags))
continue;
}
printf("%-11s", curr_string);
cprintf_in(IS_INT, " %3d", "", cpu - 1);
for (j = 0; j < ic_nr; j++) {
p0 = st_ic[curr] + j; /* irq_name set only for CPU#0 */
/*
* An empty string for irq_name means it is a remaining interrupt
* which is no longer used, for example because the
* number of interrupts has decreased in /proc/interrupts.
*/
if (p0->irq_name[0] == '\0')
/* End of the list of interrupts */
break;
q0 = st_ic[prev] + j;
offset = j;
/*
* If we want stats for the time since system startup,
* we have p0->irq_name != q0->irq_name, since q0 structure
* is completely set to zero.
*/
if (strcmp(p0->irq_name, q0->irq_name) && interval) {
/* Check if interrupt exists elsewhere in list */
for (offset = 0; offset < ic_nr; offset++) {
q0 = st_ic[prev] + offset;
if (!strcmp(p0->irq_name, q0->irq_name))
/* Interrupt found at another position */
break;
}
}
p = st_ic[curr] + (cpu - 1) * ic_nr + j;
if (!strcmp(p0->irq_name, q0->irq_name) || !interval) {
q = st_ic[prev] + (cpu - 1) * ic_nr + offset;
cprintf_f(1, colwidth[j], 2,
S_VALUE(q->interrupt, p->interrupt, itv));
}
else {
/*
* Instead of printing "N/A", assume that previous value
* for this new interrupt was zero.
*/
cprintf_f(1, colwidth[j], 2,
S_VALUE(0, p->interrupt, itv));
}
}
printf("\n");
}
}
/*
***************************************************************************
* Core function used to display statistics.
*
* IN:
* @prev Position in array where statistics used as reference are.
* Stats used as reference may be the previous ones read, or
* the very first ones when calculating the average.
* @curr Position in array where statistics for current sample are.
* @dis TRUE if a header line must be printed.
* @prev_string String displayed at the beginning of a header line. This is
* the timestamp of the previous sample, or "Average" when
* displaying average stats.
* @curr_string String displayed at the beginning of current sample stats.
* This is the timestamp of the current sample, or "Average"
* when displaying average stats.
***************************************************************************
*/
void write_stats_core(int prev, int curr, int dis,
char *prev_string, char *curr_string)
{
struct stats_cpu *scc, *scp;
unsigned long long itv, g_itv;
int cpu;
/* Test stdout */
TEST_STDOUT(STDOUT_FILENO);
/* Compute time interval */
g_itv = get_interval(uptime[prev], uptime[curr]);
/* Reduce interval value to one processor */
if (cpu_nr > 1) {
itv = get_interval(uptime0[prev], uptime0[curr]);
}
else {
itv = g_itv;
}
/* Print CPU stats */
if (DISPLAY_CPU(actflags)) {
write_cpu_stats(dis, g_itv, prev, curr, prev_string, curr_string);
}
/* Print total number of interrupts per processor */
if (DISPLAY_IRQ_SUM(actflags)) {
write_isumcpu_stats(dis, itv, prev, curr, prev_string, curr_string);
}
/* Display each interrupt value for each CPU */
if (DISPLAY_IRQ_CPU(actflags)) {
write_irqcpu_stats(st_irqcpu, irqcpu_nr, dis, itv, prev, curr,
prev_string, curr_string);
}
if (DISPLAY_SOFTIRQS(actflags)) {
write_irqcpu_stats(st_softirqcpu, softirqcpu_nr, dis, itv, prev, curr,
prev_string, curr_string);
}
/* Fix CPU counter values for every offline CPU */
for (cpu = 1; cpu <= cpu_nr; cpu++) {
scc = st_cpu[curr] + cpu;
scp = st_cpu[prev] + cpu;
if ((scc->cpu_user + scc->cpu_nice + scc->cpu_sys +
scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal +
scc->cpu_hardirq + scc->cpu_softirq) == 0) {
/*
* Offline CPU found.
* Set current struct fields (which have been set to zero)
* to values from previous iteration. Hence their values won't
* jump from zero when the CPU comes back online.
*/
*scc = *scp;
}
}
}
/*
***************************************************************************
* Print statistics average.
*
* IN:
* @curr Position in array where statistics for current sample are.
* @dis TRUE if a header line must be printed.
***************************************************************************
*/
void write_stats_avg(int curr, int dis)
{
char string[16];
strncpy(string, _("Average:"), 16);
string[15] = '\0';
write_stats_core(2, curr, dis, string, string);
}
/*
***************************************************************************
* Print statistics.
*
* IN:
* @curr Position in array where statistics for current sample are.
* @dis TRUE if a header line must be printed.
***************************************************************************
*/
void write_stats(int curr, int dis)
{
char cur_time[2][TIMESTAMP_LEN];
/* Get previous timestamp */
if (is_iso_time_fmt()) {
strftime(cur_time[!curr], sizeof(cur_time[!curr]), "%H:%M:%S", &mp_tstamp[!curr]);
}
else {
strftime(cur_time[!curr], sizeof(cur_time[!curr]), "%X", &(mp_tstamp[!curr]));
}
/* Get current timestamp */
if (is_iso_time_fmt()) {
strftime(cur_time[curr], sizeof(cur_time[curr]), "%H:%M:%S", &mp_tstamp[curr]);
}
else {
strftime(cur_time[curr], sizeof(cur_time[curr]), "%X", &(mp_tstamp[curr]));
}
write_stats_core(!curr, curr, dis, cur_time[!curr], cur_time[curr]);
}
/*
***************************************************************************
* Read stats from /proc/interrupts or /proc/softirqs.
*
* IN:
* @file /proc file to read (interrupts or softirqs).
* @ic_nr Number of interrupts (hard or soft) per CPU.
* @curr Position in array where current statistics will be saved.
*
* OUT:
* @st_ic Array for per-CPU interrupts statistics.
***************************************************************************
*/
void read_interrupts_stat(char *file, struct stats_irqcpu *st_ic[], int ic_nr, int curr)
{
FILE *fp;
struct stats_irq *st_irq_i;
struct stats_irqcpu *p;
char *line = NULL, *li;
unsigned long irq = 0;
unsigned int cpu;
int cpu_index[cpu_nr], index = 0, len;
char *cp, *next;
/* Reset total number of interrupts received by each CPU */
for (cpu = 0; cpu < cpu_nr; cpu++) {
st_irq_i = st_irq[curr] + cpu + 1;
st_irq_i->irq_nr = 0;
}
if ((fp = fopen(file, "r")) != NULL) {
SREALLOC(line, char, INTERRUPTS_LINE + 11 * cpu_nr);
/*
* Parse header line to see which CPUs are online
*/
while (fgets(line, INTERRUPTS_LINE + 11 * cpu_nr, fp) != NULL) {
next = line;
while (((cp = strstr(next, "CPU")) != NULL) && (index < cpu_nr)) {
cpu = strtol(cp + 3, &next, 10);
cpu_index[index++] = cpu;
}
if (index)
/* Header line found */
break;
}
/* Parse each line of interrupts statistics data */
while ((fgets(line, INTERRUPTS_LINE + 11 * cpu_nr, fp) != NULL) &&
(irq < ic_nr)) {
/* Skip over "<irq>:" */
if ((cp = strchr(line, ':')) == NULL)
/* Chr ':' not found */
continue;
cp++;
p = st_ic[curr] + irq;
/* Remove possible heading spaces in interrupt's name... */
li = line;
while (*li == ' ')
li++;
len = strcspn(li, ":");
if (len >= MAX_IRQ_LEN) {
len = MAX_IRQ_LEN - 1;
}
/* ...then save its name */
strncpy(p->irq_name, li, len);
p->irq_name[len] = '\0';
/* For each interrupt: Get number received by each CPU */
for (cpu = 0; cpu < index; cpu++) {
p = st_ic[curr] + cpu_index[cpu] * ic_nr + irq;
st_irq_i = st_irq[curr] + cpu_index[cpu] + 1;
/*
* No need to set (st_irqcpu + cpu * irqcpu_nr)->irq_name:
* This is the same as st_irqcpu->irq_name.
* Now save current interrupt value for current CPU (in stats_irqcpu structure)
* and total number of interrupts received by current CPU (in stats_irq structure).
*/
p->interrupt = strtoul(cp, &next, 10);
st_irq_i->irq_nr += p->interrupt;
cp = next;
}
irq++;
}
fclose(fp);
free(line);
}
while (irq < ic_nr) {
/* Nb of interrupts per processor has changed */
p = st_ic[curr] + irq;
p->irq_name[0] = '\0'; /* This value means this is a dummy interrupt */
irq++;
}
}
/*
***************************************************************************
* Main loop: Read stats from the relevant sources, and display them.
*
* IN:
* @dis_hdr Set to TRUE if the header line must always be printed.
* @rows Number of rows of screen.
***************************************************************************
*/
void rw_mpstat_loop(int dis_hdr, int rows)
{
struct stats_cpu *scc;
int cpu;
int curr = 1, dis = 1;
unsigned long lines = rows;
/* Dont buffer data if redirected to a pipe */
setbuf(stdout, NULL);
/* Read uptime and CPU stats */
if (cpu_nr > 1) {
/*
* Init uptime0. So if /proc/uptime cannot fill it,
* this will be done by /proc/stat.
*/
uptime0[0] = 0;
read_uptime(&(uptime0[0]));
}
read_stat_cpu(st_cpu[0], cpu_nr + 1, &(uptime[0]), &(uptime0[0]));
/*
* Read total number of interrupts received among all CPU.
* (this is the first value on the line "intr:" in the /proc/stat file).
*/
if (DISPLAY_IRQ_SUM(actflags)) {
read_stat_irq(st_irq[0], 1);
}
/*
* Read number of interrupts received by each CPU, for each interrupt,
* and compute the total number of interrupts received by each CPU.
*/
if (DISPLAY_IRQ_SUM(actflags) || DISPLAY_IRQ_CPU(actflags)) {
/* Read this file to display int per CPU or total nr of int per CPU */
read_interrupts_stat(INTERRUPTS, st_irqcpu, irqcpu_nr, 0);
}
if (DISPLAY_SOFTIRQS(actflags)) {
read_interrupts_stat(SOFTIRQS, st_softirqcpu, softirqcpu_nr, 0);
}
if (!interval) {
/* Display since boot time */
mp_tstamp[1] = mp_tstamp[0];
memset(st_cpu[1], 0, STATS_CPU_SIZE * (cpu_nr + 1));
memset(st_irq[1], 0, STATS_IRQ_SIZE * (cpu_nr + 1));
memset(st_irqcpu[1], 0, STATS_IRQCPU_SIZE * (cpu_nr + 1) * irqcpu_nr);
if (DISPLAY_SOFTIRQS(actflags)) {
memset(st_softirqcpu[1], 0, STATS_IRQCPU_SIZE * (cpu_nr + 1) * softirqcpu_nr);
}
write_stats(0, DISP_HDR);
exit(0);
}
/* Set a handler for SIGALRM */
memset(&alrm_act, 0, sizeof(alrm_act));
alrm_act.sa_handler = alarm_handler;
sigaction(SIGALRM, &alrm_act, NULL);
alarm(interval);
/* Save the first stats collected. Will be used to compute the average */
mp_tstamp[2] = mp_tstamp[0];
uptime[2] = uptime[0];
uptime0[2] = uptime0[0];
memcpy(st_cpu[2], st_cpu[0], STATS_CPU_SIZE * (cpu_nr + 1));
memcpy(st_irq[2], st_irq[0], STATS_IRQ_SIZE * (cpu_nr + 1));
memcpy(st_irqcpu[2], st_irqcpu[0], STATS_IRQCPU_SIZE * (cpu_nr + 1) * irqcpu_nr);
if (DISPLAY_SOFTIRQS(actflags)) {
memcpy(st_softirqcpu[2], st_softirqcpu[0],
STATS_IRQCPU_SIZE * (cpu_nr + 1) * softirqcpu_nr);
}
/* Set a handler for SIGINT */
memset(&int_act, 0, sizeof(int_act));
int_act.sa_handler = int_handler;
sigaction(SIGINT, &int_act, NULL);
pause();
if (sigint_caught)
/* SIGINT signal caught during first interval: Exit immediately */
return;
do {
/*
* Resetting the structure not needed since every fields will be set.
* Exceptions are per-CPU structures: Some of them may not be filled
* if corresponding processor is disabled (offline). We set them to zero
* to be able to distinguish between offline and tickless CPUs.
*/
for (cpu = 1; cpu <= cpu_nr; cpu++) {
scc = st_cpu[curr] + cpu;
memset(scc, 0, STATS_CPU_SIZE);
}
/* Get time */
get_localtime(&(mp_tstamp[curr]), 0);
/* Read uptime and CPU stats */
if (cpu_nr > 1) {
uptime0[curr] = 0;
read_uptime(&(uptime0[curr]));
}
read_stat_cpu(st_cpu[curr], cpu_nr + 1, &(uptime[curr]), &(uptime0[curr]));
/* Read total number of interrupts received among all CPU */
if (DISPLAY_IRQ_SUM(actflags)) {
read_stat_irq(st_irq[curr], 1);
}
/*
* Read number of interrupts received by each CPU, for each interrupt,
* and compute the total number of interrupts received by each CPU.
*/
if (DISPLAY_IRQ_SUM(actflags) || DISPLAY_IRQ_CPU(actflags)) {
read_interrupts_stat(INTERRUPTS, st_irqcpu, irqcpu_nr, curr);
}
if (DISPLAY_SOFTIRQS(actflags)) {
read_interrupts_stat(SOFTIRQS, st_softirqcpu, softirqcpu_nr, curr);
}
/* Write stats */
if (!dis_hdr) {
dis = lines / rows;
if (dis) {
lines %= rows;
}
lines++;
}
write_stats(curr, dis);
if (count > 0) {
count--;
}
if (count) {
pause();
if (sigint_caught) {
/* SIGINT signal caught => Display average stats */
count = 0;
printf("\n"); /* Skip "^C" displayed on screen */
}
else {
curr ^= 1;
}
}
}
while (count);
/* Write stats average */
write_stats_avg(curr, dis_hdr);
}
/*
***************************************************************************
* Main entry to the program
***************************************************************************
*/
int main(int argc, char **argv)
{
int opt = 0, i, actset = FALSE;
struct utsname header;
int dis_hdr = -1;
int rows = 23;
char *t;
#ifdef USE_NLS
/* Init National Language Support */
init_nls();
#endif
/* Init color strings */
init_colors();
/* Get HZ */
get_HZ();
/* What is the highest processor number on this machine? */
cpu_nr = get_cpu_nr(~0, TRUE);
/* Calculate number of interrupts per processor */
irqcpu_nr = get_irqcpu_nr(INTERRUPTS, NR_IRQS, cpu_nr) +
NR_IRQCPU_PREALLOC;
/* Calculate number of soft interrupts per processor */
softirqcpu_nr = get_irqcpu_nr(SOFTIRQS, NR_IRQS, cpu_nr) +
NR_IRQCPU_PREALLOC;
/*
* cpu_nr: a value of 2 means there are 2 processors (0 and 1).
* In this case, we have to allocate 3 structures: global, proc0 and proc1.
*/
salloc_mp_struct(cpu_nr + 1);
while (++opt < argc) {
if (!strcmp(argv[opt], "-I")) {
if (argv[++opt]) {
actset = TRUE;
for (t = strtok(argv[opt], ","); t; t = strtok(NULL, ",")) {
if (!strcmp(t, K_SUM)) {
/* Display total number of interrupts per CPU */
actflags |= M_D_IRQ_SUM;
}
else if (!strcmp(t, K_CPU)) {
/* Display interrupts per CPU */
actflags |= M_D_IRQ_CPU;
}
else if (!strcmp(t, K_SCPU)) {
/* Display soft interrupts per CPU */
actflags |= M_D_SOFTIRQS;
}
else if (!strcmp(t, K_ALL)) {
actflags |= M_D_IRQ_SUM + M_D_IRQ_CPU + M_D_SOFTIRQS;
}
else {
usage(argv[0]);
}
}
}
else {
usage(argv[0]);
}
}
else if (!strcmp(argv[opt], "-P")) {
/* '-P ALL' can be used on UP machines */
if (argv[++opt]) {
flags |= F_P_OPTION;
dis_hdr++;
for (t = strtok(argv[opt], ","); t; t = strtok(NULL, ",")) {
if (!strcmp(t, K_ALL) || !strcmp(t, K_ON)) {
if (cpu_nr) {
dis_hdr = 9;
}
/*
* Set bit for every processor.
* Also indicate to display stats for CPU 'all'.
*/
memset(cpu_bitmap, 0xff, ((cpu_nr + 1) >> 3) + 1);
if (!strcmp(t, K_ON)) {
/* Display stats only for online CPU */
flags |= F_P_ON;
}
}
else {
if (strspn(t, DIGITS) != strlen(t)) {
usage(argv[0]);
}
i = atoi(t); /* Get cpu number */
if (i >= cpu_nr) {
fprintf(stderr, _("Not that many processors!\n"));
exit(1);
}
i++;
*(cpu_bitmap + (i >> 3)) |= 1 << (i & 0x07);
}
}
}
else {
usage(argv[0]);
}
}
else if (!strncmp(argv[opt], "-", 1)) {
for (i = 1; *(argv[opt] + i); i++) {
switch (*(argv[opt] + i)) {
case 'A':
actflags |= M_D_CPU + M_D_IRQ_SUM + M_D_IRQ_CPU + M_D_SOFTIRQS;
actset = TRUE;
/* Select all processors */
flags |= F_P_OPTION;
memset(cpu_bitmap, 0xff, ((cpu_nr + 1) >> 3) + 1);
break;
case 'u':
/* Display CPU */
actflags |= M_D_CPU;
break;
case 'V':
/* Print version number */
print_version();
break;
default:
usage(argv[0]);
}
}
}
else if (interval < 0) {
/* Get interval */
if (strspn(argv[opt], DIGITS) != strlen(argv[opt])) {
usage(argv[0]);
}
interval = atol(argv[opt]);
if (interval < 0) {
usage(argv[0]);
}
count = -1;
}
else if (count <= 0) {
/* Get count value */
if ((strspn(argv[opt], DIGITS) != strlen(argv[opt])) ||
!interval) {
usage(argv[0]);
}
count = atol(argv[opt]);
if (count < 1) {
usage(argv[0]);
}
}
else {
usage(argv[0]);
}
}
/* Default: Display CPU */
if (!actset) {
actflags |= M_D_CPU;
}
if (count_bits(&actflags, sizeof(unsigned int)) > 1) {
dis_hdr = 9;
}
if (!USE_P_OPTION(flags)) {
/* Option -P not used: Set bit 0 (global stats among all proc) */
*cpu_bitmap = 1;
}
if (dis_hdr < 0) {
dis_hdr = 0;
}
if (!dis_hdr) {
/* Get window size */
rows = get_win_height();
}
if (interval < 0) {
/* Interval not set => display stats since boot time */
interval = 0;
}
/* Get time */
get_localtime(&(mp_tstamp[0]), 0);
/* Get system name, release number and hostname */
uname(&header);
print_gal_header(&(mp_tstamp[0]), header.sysname, header.release,
header.nodename, header.machine, get_cpu_nr(~0, FALSE));
/* Main loop */
rw_mpstat_loop(dis_hdr, rows);
/* Free structures */
sfree_mp_struct();
return 0;
}
|