source: trunk/third/libgtop/examples/smp.c @ 18532

Revision 18532, 4.3 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18531, which included commits to RCS files with non-trunk default branches.
Line 
1/* $Id: smp.c,v 1.1.1.2 2003-01-27 03:24:10 ghudson Exp $ */
2
3/* Copyright (C) 1998-99 Martin Baulig
4   This file is part of LibGTop 1.0.
5
6   Contributed by Martin Baulig <martin@home-of-linux.org>, September 1998.
7
8   LibGTop is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   LibGTop is distributed in the hope that it will be useful, but WITHOUT
14   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16   for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with LibGTop; see the file COPYING. If not, write to the
20   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21   Boston, MA 02111-1307, USA.
22*/
23
24#include <locale.h>
25#include <math.h>
26
27#include <glibtop.h>
28#include <glibtop/cpu.h>
29#include <glibtop/xmalloc.h>
30
31int
32main (int argc, char *argv [])
33{
34        glibtop_cpu cpu;
35        unsigned long frequency;
36        double total, user, nice, sys, idle;
37        double b_total, b_user, b_nice, b_sys, b_idle;
38        double s_total, s_user, s_nice, s_sys, s_idle;
39        char separator [BUFSIZ], buffer [BUFSIZ];
40        int ncpu, i;
41
42        setlocale (LC_ALL, "");
43        bindtextdomain (GETTEXT_PACKAGE, GTOPLOCALEDIR);
44        textdomain (GETTEXT_PACKAGE);
45       
46        glibtop_init();
47
48        glibtop_get_cpu (&cpu);
49
50        ncpu = glibtop_global_server->ncpu ? glibtop_global_server->ncpu : 1;
51
52        frequency = (unsigned long) cpu.frequency;
53
54        total = ((unsigned long) cpu.total) ? ((double) cpu.total) : 1.0;
55        user  = ((unsigned long) cpu.user)  ? ((double) cpu.user)  : 1.0;
56        nice  = ((unsigned long) cpu.nice)  ? ((double) cpu.nice)  : 1.0;
57        sys   = ((unsigned long) cpu.sys)   ? ((double) cpu.sys)   : 1.0;
58        idle  = ((unsigned long) cpu.idle)  ? ((double) cpu.idle)  : 1.0;
59
60        s_total = s_user = s_nice = s_sys = s_idle = 0.0;
61
62        b_total = total / ncpu;
63        b_user  = user  / ncpu;
64        b_nice  = nice  / ncpu;
65        b_sys   = sys   / ncpu;
66        b_idle  = idle  / ncpu;
67
68        memset (separator, '-', 91);
69        separator [92] = '\0';
70
71        sprintf (buffer, _("Ticks (%ld per second):"), frequency);
72
73        printf ("\n\n%-26s %12s %12s %12s %12s %12s\n%s\n", buffer,
74                _("Total"), _("User"), _("Nice"), _("Sys"), _("Idle"), separator);
75
76        printf (_("CPU          (0x%08lx): %12.0f %12.0f %12.0f %12.0f %12.0f\n\n"),
77                (unsigned long) cpu.flags, total, user, nice, sys, idle);
78
79        for (i = 0; i < glibtop_global_server->ncpu; i++) {
80                printf (_("CPU %3d      (0x%08lx): %12lu %12lu %12lu %12lu %12lu\n"), i,
81                        (unsigned long) cpu.flags,
82                        (unsigned long) cpu.xcpu_total [i],
83                        (unsigned long) cpu.xcpu_user  [i],
84                        (unsigned long) cpu.xcpu_nice  [i],
85                        (unsigned long) cpu.xcpu_sys   [i],
86                        (unsigned long) cpu.xcpu_idle  [i]);
87
88                s_total += fabs (((double) cpu.xcpu_total [i]) - b_total);
89                s_user  += fabs (((double) cpu.xcpu_user  [i]) - b_user);
90                s_nice  += fabs (((double) cpu.xcpu_nice  [i]) - b_nice);
91                s_sys   += fabs (((double) cpu.xcpu_sys   [i]) - b_sys);
92                s_idle  += fabs (((double) cpu.xcpu_idle  [i]) - b_idle);
93        }
94
95        printf ("%s\n\n\n", separator);
96
97        printf ("%-26s %12s %12s %12s %12s %12s\n%s\n", _("Percent:"),
98                _("Total (%)"), _("User (%)"), _("Nice (%)"), _("Sys (%)"),
99                _("Idle (%)"), separator);
100       
101        printf (_("CPU          (0x%08lx): %12.3f %12.3f %12.3f %12.3f %12.3f\n\n"),
102                (unsigned long) cpu.flags, (double) total * 100.0 / total,
103                (double) user  * 100.0 / total,
104                (double) nice  * 100.0 / total,
105                (double) sys   * 100.0 / total,
106                (double) idle  * 100.0 / total);
107
108        for (i = 0; i < glibtop_global_server->ncpu; i++) {
109                double p_total, p_user, p_nice, p_sys, p_idle;
110
111                p_total = ((double) cpu.xcpu_total [i]) * 100.0 / total;
112                p_user  = ((double) cpu.xcpu_user  [i]) * 100.0 / user;
113                p_nice  = ((double) cpu.xcpu_nice  [i]) * 100.0 / nice;
114                p_sys   = ((double) cpu.xcpu_sys   [i]) * 100.0 / sys;
115                p_idle  = ((double) cpu.xcpu_idle  [i]) * 100.0 / idle;
116
117                printf (_("CPU %3d      (0x%08lx): %12.3f %12.3f %12.3f %12.3f %12.3f\n"),
118                        i, (unsigned long) cpu.flags, p_total, p_user, p_nice,
119                        p_sys, p_idle);
120        }
121
122        printf ("%s\n%-26s %12.3f %12.3f %12.3f %12.3f %12.3f\n\n", separator,
123                _("Spin:"), s_total * 100.0 / total, s_user * 100.0 / user,
124                s_nice * 100.0 / nice, s_sys * 100.0 / sys, s_idle * 100.0 / idle);
125
126        exit (0);
127}
Note: See TracBrowser for help on using the repository browser.