File: longrun.c

package info (click to toggle)
procmeter3 3.6-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,444 kB
  • sloc: ansic: 16,600; makefile: 428; sh: 13
file content (224 lines) | stat: -rw-r--r-- 6,140 bytes parent folder | download | duplicates (3)
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
/***************************************
  $Header: /home/amb/CVS/procmeter3/modules/longrun.c,v 1.10 2010-02-28 10:22:16 amb Exp $

  ProcMeter - A system monitoring program for Linux - Version 3.5d.

  Transmeta longrun support.
  ******************/ /******************
  Written by Joey Hess (with heavy borrowing from longrun)

  This file Copyright 2002 Joey Hess
            Copyright 2001 Transmeta Corporation
  It may be distributed under the GNU Public License, version 2, or
  any higher version.  See section COPYING of the GNU Public license
  for conditions under which this file may be redistributed.
  ***************************************/

#define _XOPEN_SOURCE 500
#define _DEFAULT_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define __USE_FILE_OFFSET64 /* we should use 64 bit offset for pread */
#include <unistd.h>

#include "procmeter.h"

/* The canonical source for these defines is longrun.c in the longrun
 * utility. */
#define CPUID_DEVICE "/dev/cpu/0/cpuid"
#define CPUID_TMx86_LONGRUN_STATUS 0x80860007
#define CPUID_TMx86_VENDOR_ID 0x80860000
#define CPUID_TMx86_PROCESSOR_INFO 0x80860001
#define CPUID_TMx86_FEATURE_LONGRUN(x) ((x) & 0x02)

static int cpuid_fd = 0;

static void read_cpuid(loff_t address, int *eax, int *ebx, int *ecx, int *edx) {
        uint32_t data[4];

        if (pread(cpuid_fd, &data, 16, address) != 16) {
                perror("error reading");
        }

        if (eax) *eax = data[0];
        if (ebx) *ebx = data[1];
        if (ecx) *ecx = data[2];
        if (edx) *edx = data[3];
}

/* The interface information.  */

/*+ The template for the longrun devices +*/
ProcMeterOutput _outputs[1]=
{
 {
  /* char  name[];          */ "Longrun",
  /* char *description;     */ "current longrun performance level",
  /* char  type;            */ PROCMETER_GRAPH|PROCMETER_TEXT|PROCMETER_BAR,
  /* short interval;        */ 1,
  /* char  text_value[];    */ "0 %",
  /* long  graph_value;     */ 0,
  /* short graph_scale;     */ 20,
  /* char  graph_units[];   */ "(%d%%)"
 },
};

static int ndevices=0;
static unsigned long *current=NULL,*previous=NULL;
static char **device=NULL;

static void add_device(void);

/*+ The outputs. +*/
ProcMeterOutput **outputs=NULL;

/*+ The module. +*/
ProcMeterModule module=
{
 /* char name[];            */ "Longrun",
 /* char *description;      */ "Transmeta Crusoe longrun information.  "
                               "Only available if using a Transmeta Crusoe CPU that supports it and the kernel was compiled with CONFIG_X86_CPUID=y."
};

/*++++++++++++++++++++++++++++++++++++++
  Load the module.

  ProcMeterModule *Load Returns the module information.
  ++++++++++++++++++++++++++++++++++++++*/

ProcMeterModule *Load(void)
{
 return(&module);
}


/*++++++++++++++++++++++++++++++++++++++
  Initialise the module, creating the outputs as required.

  ProcMeterOutput **Initialise Returns a NULL terminated list of outputs.

  char *options The options string for the module from the .procmeterrc file.
  ++++++++++++++++++++++++++++++++++++++*/

ProcMeterOutput **Initialise(char *options)
{
 int eax, ebx, ecx, edx;

 outputs=(ProcMeterOutput**)malloc(sizeof(ProcMeterOutput*));
 outputs[0]=NULL;

 if ((cpuid_fd = open(CPUID_DEVICE, O_RDONLY)) < 0) {
  /* Don't bother giving an error message for 99% of systems. */
  //	 fprintf(stderr, "ProcMeter(%s): Cannot open " CPUID_DEVICE ". make sure your kernel was compiled with CONFIG_X86_CPUID=y, and make sure the device is readable\n", __FILE__);
	 return outputs;
 }
 
 /* See if longrun is supported by this system. */
 read_cpuid(CPUID_TMx86_VENDOR_ID, &eax, &ebx, &ecx, &edx);
 if (ebx != 0x6e617254 || ecx != 0x55504361 || edx != 0x74656d73) {
	 fprintf(stderr, "ProcMeter(%s): Not a Transmeta x86 CPU.\n", __FILE__);
	 return outputs;
 }
 read_cpuid(CPUID_TMx86_PROCESSOR_INFO, &eax, &ebx, &ecx, &edx);
 if (!CPUID_TMx86_FEATURE_LONGRUN(edx)) {
	 fprintf(stderr, "ProcMeter(%s): Longrun unsupported.\n", __FILE__);
	 return outputs;
 }
 
 add_device();

 current =(unsigned long*)malloc(sizeof(long)*ndevices);
 previous=(unsigned long*)malloc(sizeof(long)*ndevices);

 return(outputs);
}

/*++++++++++++++++++++++++++++++++++++++
  Add a new device to the list.

  Currently we just support one CPU, so no parameters.
  ++++++++++++++++++++++++++++++++++++++*/

static void add_device(void)
{
 int nstats=1;
 int i;

 outputs=(ProcMeterOutput**)realloc((void*)outputs,(ndevices+nstats+1)*sizeof(ProcMeterOutput*));
 device=(char**)realloc((void*)device,(ndevices+nstats+1)*sizeof(char*));

 for(i=0;nstats;nstats--)
   {
    outputs[ndevices]=(ProcMeterOutput*)malloc(sizeof(ProcMeterOutput));
    device[ndevices]=(char*)malloc(1);

    *outputs[ndevices]=_outputs[i];
    outputs[ndevices]->description=(char*)malloc(strlen(_outputs[i].description)+4);
    strcpy(outputs[ndevices]->description,_outputs[i].description);

    strcpy(device[ndevices],"0");

    ndevices++;

    i++;
   }

 outputs[ndevices]=NULL;
}

/*++++++++++++++++++++++++++++++++++++++
  Perform an update on one of the statistics.

  int Update Returns 0 if OK, else -1.

  time_t now The current time.

  ProcMeterOutput *output The output that the value is wanted for.
  ++++++++++++++++++++++++++++++++++++++*/

int Update(time_t now,ProcMeterOutput *output)
{
 int percent;
 read_cpuid(CPUID_TMx86_LONGRUN_STATUS, 0, 0, &percent, 0);
 output[0].graph_value=PROCMETER_GRAPH_FLOATING(percent/output[0].graph_scale);
 sprintf(output->text_value,"%i %%",percent);
 return(0);
}


/*++++++++++++++++++++++++++++++++++++++
  Tidy up and prepare to have the module unloaded.
  ++++++++++++++++++++++++++++++++++++++*/

void Unload(void)
{
 int i;

 if(outputs)
   {
    for(i=0;outputs[i];i++)
      {
       free(outputs[i]->description);
       free(outputs[i]);
      }
    free(outputs);
   }
 if(current)
    free(current);
 if(previous)
    free(previous);
 if(device)
   {
    for(i=0;i<ndevices;i++)
       free(device[i]);
    free(device);
   }
 if (cpuid_fd)
   close(cpuid_fd);
}