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

Revision 20897, 4.3 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r20896, which included commits to RCS files with non-trunk default branches.
Line 
1/* $Id: smp.c,v 1.1.1.3 2004-10-03 05:00:40 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 <config.h>
25
26#include <locale.h>
27#include <libintl.h>
28#include <math.h>
29#include <stdio.h>
30
31#include <glibtop.h>
32#include <glibtop/cpu.h>
33
34#include "libgtop-i18n.h"
35
36int
37main (int argc, char *argv [])
38{
39        glibtop_cpu cpu;
40        unsigned long frequency;
41        double total, user, nice, sys, idle;
42        double b_total, b_user, b_nice, b_sys, b_idle;
43        double s_total, s_user, s_nice, s_sys, s_idle;
44        char separator [BUFSIZ], buffer [BUFSIZ];
45        int ncpu, i;
46
47        setlocale (LC_ALL, "");
48        bindtextdomain (GETTEXT_PACKAGE, GTOPLOCALEDIR);
49        textdomain (GETTEXT_PACKAGE);
50
51        glibtop_init();
52
53        glibtop_get_cpu (&cpu);
54
55        ncpu = glibtop_global_server->ncpu ? glibtop_global_server->ncpu : 1;
56
57        frequency = (unsigned long) cpu.frequency;
58
59        total = ((unsigned long) cpu.total) ? ((double) cpu.total) : 1.0;
60        user  = ((unsigned long) cpu.user)  ? ((double) cpu.user)  : 1.0;
61        nice  = ((unsigned long) cpu.nice)  ? ((double) cpu.nice)  : 1.0;
62        sys   = ((unsigned long) cpu.sys)   ? ((double) cpu.sys)   : 1.0;
63        idle  = ((unsigned long) cpu.idle)  ? ((double) cpu.idle)  : 1.0;
64
65        s_total = s_user = s_nice = s_sys = s_idle = 0.0;
66
67        b_total = total / ncpu;
68        b_user  = user  / ncpu;
69        b_nice  = nice  / ncpu;
70        b_sys   = sys   / ncpu;
71        b_idle  = idle  / ncpu;
72
73        memset (separator, '-', 91);
74        separator [92] = '\0';
75
76        sprintf (buffer, _("Ticks (%ld per second):"), frequency);
77
78        printf ("\n\n%-26s %12s %12s %12s %12s %12s\n%s\n", buffer,
79                _("Total"), _("User"), _("Nice"), _("Sys"), _("Idle"), separator);
80
81        printf (_("CPU          (0x%08lx): %12.0f %12.0f %12.0f %12.0f %12.0f\n\n"),
82                (unsigned long) cpu.flags, total, user, nice, sys, idle);
83
84        for (i = 0; i < glibtop_global_server->ncpu; i++) {
85                printf (_("CPU %3d      (0x%08lx): %12lu %12lu %12lu %12lu %12lu\n"), i,
86                        (unsigned long) cpu.flags,
87                        (unsigned long) cpu.xcpu_total [i],
88                        (unsigned long) cpu.xcpu_user  [i],
89                        (unsigned long) cpu.xcpu_nice  [i],
90                        (unsigned long) cpu.xcpu_sys   [i],
91                        (unsigned long) cpu.xcpu_idle  [i]);
92
93                s_total += fabs (((double) cpu.xcpu_total [i]) - b_total);
94                s_user  += fabs (((double) cpu.xcpu_user  [i]) - b_user);
95                s_nice  += fabs (((double) cpu.xcpu_nice  [i]) - b_nice);
96                s_sys   += fabs (((double) cpu.xcpu_sys   [i]) - b_sys);
97                s_idle  += fabs (((double) cpu.xcpu_idle  [i]) - b_idle);
98        }
99
100        printf ("%s\n\n\n", separator);
101
102        printf ("%-26s %12s %12s %12s %12s %12s\n%s\n", _("Percent:"),
103                _("Total (%)"), _("User (%)"), _("Nice (%)"), _("Sys (%)"),
104                _("Idle (%)"), separator);
105
106        printf (_("CPU          (0x%08lx): %12.3f %12.3f %12.3f %12.3f %12.3f\n\n"),
107                (unsigned long) cpu.flags, (double) total * 100.0 / total,
108                (double) user  * 100.0 / total,
109                (double) nice  * 100.0 / total,
110                (double) sys   * 100.0 / total,
111                (double) idle  * 100.0 / total);
112
113        for (i = 0; i < glibtop_global_server->ncpu; i++) {
114                double p_total, p_user, p_nice, p_sys, p_idle;
115
116                p_total = ((double) cpu.xcpu_total [i]) * 100.0 / total;
117                p_user  = ((double) cpu.xcpu_user  [i]) * 100.0 / user;
118                p_nice  = ((double) cpu.xcpu_nice  [i]) * 100.0 / nice;
119                p_sys   = ((double) cpu.xcpu_sys   [i]) * 100.0 / sys;
120                p_idle  = ((double) cpu.xcpu_idle  [i]) * 100.0 / idle;
121
122                printf (_("CPU %3d      (0x%08lx): %12.3f %12.3f %12.3f %12.3f %12.3f\n"),
123                        i, (unsigned long) cpu.flags, p_total, p_user, p_nice,
124                        p_sys, p_idle);
125        }
126
127        printf ("%s\n%-26s %12.3f %12.3f %12.3f %12.3f %12.3f\n\n", separator,
128                _("Spin:"), s_total * 100.0 / total, s_user * 100.0 / user,
129                s_nice * 100.0 / nice, s_sys * 100.0 / sys, s_idle * 100.0 / idle);
130
131        exit (0);
132}
Note: See TracBrowser for help on using the repository browser.