~ubuntu-branches/ubuntu/trusty/linuxlogo/trusty

« back to all changes in this revision

Viewing changes to libsysinfo-0.1.0/Linux/cpuinfo_x86.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-06-20 09:19:00 UTC
  • mfrom: (4.1.2 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080620091900-4xzuv7c7ntxvs7wt
Tags: 5.03-4
* Adding patch to fix FTBFS on s390x.
* Updating to standards 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Re-written from scratch 3 March 2001      */
2
 
/* Handles intel chips on Linux architecture */
3
 
/* by Vince Weaver <vince@deater.net>        */
4
 
 
5
 
#include <stdio.h>
6
 
#include <string.h>
7
 
#include <stdlib.h>  /* atof */
8
 
 
9
 
#include "../sysinfo.h"
10
 
#include "../include/generic.h"
11
 
 
12
 
int get_cpu_info(cpu_info_t *cpu_info) {
13
 
 
14
 
    FILE *fff;
15
 
    char temp_string[256];
16
 
    char vendor_string[256],model_string[256],plain_model[256];
17
 
    int family=0;
18
 
    int cpu_count=0;
19
 
    float megahertz=0.0,bogomips=0.0;
20
 
   
21
 
    vendor_string[0]=model_string[0]=plain_model[0]=0;
22
 
 
23
 
       /* We get all of our info here from /proc/cpuinfo */
24
 
    if ((fff=fopen(get_cpuinfo_file(),"r") )!=NULL) {
25
 
       
26
 
       while ( (fgets(temp_string,255,fff)!=NULL) ) {
27
 
             
28
 
          if (cpu_count==0) {   /* Assume all CPU's in SMP system are the same */
29
 
             if ( !(strncmp(temp_string,"vendor_id",9)) ||
30
 
                  !(strncmp(temp_string,"vid",3)) ) { /* 1.2.13 kernels */
31
 
                strncpy(vendor_string,parse_line(temp_string),255);
32
 
             }
33
 
     
34
 
             if ( !(strncmp(temp_string,"model name",9)) ) {
35
 
                strncpy(model_string,parse_line(temp_string),255);
36
 
                clip_lf(model_string,255);
37
 
             }
38
 
             else {  /* for older (pre 2.2.x) kernels */
39
 
                if (!(strncmp(temp_string,"model",5))) {
40
 
                   if (strncmp(parse_line(temp_string),"unknown",7)) {
41
 
                      strncpy(plain_model,parse_line(temp_string),255);   
42
 
                      clip_lf(plain_model,255);
43
 
                   }
44
 
                }
45
 
             }
46
 
             
47
 
             if ( !(strncmp(temp_string,"cpu MHz",6))) {
48
 
                megahertz=atof(parse_line(temp_string));
49
 
             }
50
 
             
51
 
                /* We use this for cpu's w/o cpuinfo (386s and 486s) */
52
 
             if ( !(strncmp(temp_string,"cpu family",10))) {
53
 
                family=atoi(parse_line(temp_string));
54
 
             }
55
 
                /* Old legacy stuff (2.0.x kernels and earlier) */
56
 
             if ( !(strncmp(temp_string,"cpu   ",5))) {
57
 
                strncpy(plain_model,parse_line(temp_string),255);
58
 
                clip_lf(plain_model,255);
59
 
             }
60
 
             
61
 
          }
62
 
             /* Ugh why must people play with capitalization */
63
 
          if ( !(strncmp(temp_string,"bogomips",8)) ||
64
 
               !(strncmp(temp_string,"BogoMips",8)) ||
65
 
               !(strncmp(temp_string,"BogoMIPS",8))) {
66
 
             bogomips+=atof(parse_line(temp_string));
67
 
             cpu_count++;  /* Cheating way to detect number of intel CPU's */
68
 
          }
69
 
       }
70
 
    }
71
 
       
72
 
       /* Re-arrange some of the strings for best results  */
73
 
    if (model_string[0]==0) {  /* For 1.2.13 Kernels */
74
 
       if (strlen(plain_model)>1) strncpy(model_string,plain_model,63);
75
 
    }
76
 
 
77
 
    if (model_string[0]==0) {
78
 
       strncpy(cpu_info->chip_type,"Unknown",9);
79
 
    }
80
 
    else strncpy(cpu_info->chip_type,model_string,63);
81
 
   
82
 
    strncpy(cpu_info->chip_vendor,"Unknown",9);
83
 
  
84
 
    /* sanity */
85
 
    if (cpu_count<1) cpu_count=1;
86
 
   
87
 
    cpu_info->num_cpus=cpu_count;
88
 
    cpu_info->bogomips=bogomips;
89
 
   
90
 
    /*********************************************************/
91
 
    /* Vendor specific fixups                                */
92
 
    /*********************************************************/
93
 
   
94
 
       
95
 
       /* ************** */
96
 
       /* AMD chips      */
97
 
       /* ************** */
98
 
    if (!strncmp(vendor_string,"AuthenticAMD",12)) {
99
 
       strncpy(cpu_info->chip_vendor,"AMD",4);
100
 
 
101
 
       if (!(strncmp(model_string,"AMD Sempron",11))) {
102
 
         strncpy(cpu_info->chip_type,"Sempron",8);
103
 
       }
104
 
 
105
 
          /* Clean-up K6 model info */
106
 
       if (strstr(model_string,"K6")!=NULL) {
107
 
             /* Default to K6 */
108
 
          strncpy(cpu_info->chip_type,"K6",3);
109
 
      
110
 
             /* Original K6 */
111
 
          if ( !(strncmp(model_string,"AMD-K6tm",8)))
112
 
             strncpy(cpu_info->chip_type,"K6",3);
113
 
        
114
 
             /* Counter-intuitive, but the following are correct */
115
 
          if ( !(strncmp(model_string,"AMD-K6(tm)-III",14)))
116
 
             strncpy(cpu_info->chip_type,"K6-2+",6);
117
 
          else if ( !(strncmp(model_string,"AMD-K6(tm)",10)))
118
 
             strncpy(cpu_info->chip_type,"K6-2",5);
119
 
          if ( !(strncmp(model_string,"K6-2",4)))
120
 
             strncpy(cpu_info->chip_type,"K6-2",5);
121
 
          if (strstr(model_string,"3D+")!=NULL) {
122
 
             strncpy(cpu_info->chip_type,"K6-III",7);
123
 
          }
124
 
       }
125
 
     
126
 
          /* K5 Chips */
127
 
       if (strstr(model_string,"K5")!=NULL) {
128
 
          strncpy(cpu_info->chip_type,"K5",3);
129
 
       }
130
 
 
131
 
          /* Some old K7's were reported as such */
132
 
       if (strstr(model_string,"K7")!=NULL) {
133
 
          strncpy(cpu_info->chip_type,"Athlon",7);
134
 
       }
135
 
 
136
 
          /* Athlons */
137
 
       if (strstr(model_string,"Athlon(tm) 64")!=NULL) {
138
 
          strncpy(cpu_info->chip_type,"Athlon 64",10);
139
 
       } else
140
 
       if (strstr(model_string,"Athlon")!=NULL) {
141
 
          strncpy(cpu_info->chip_type,"Athlon",7);
142
 
       }
143
 
       
144
 
          /* Specialty Athlons */
145
 
          /* Should we support MP too?  */
146
 
          /* Should we decode the Speed ratings too (ie, 1600+) */
147
 
       if (!strncmp(model_string,"AMD Athlon",10)) {
148
 
          if (strstr(model_string,"XP")!=NULL) {
149
 
             strncpy(cpu_info->chip_type,"Athlon XP",10);
150
 
          }
151
 
       }
152
 
 
153
 
          /* Durons */
154
 
       if (strstr(model_string,"Duron")!=NULL) {
155
 
          strncpy(cpu_info->chip_type,"Duron",6);
156
 
       }
157
 
       
158
 
       if (strstr(model_string,"Unknown")!=NULL) {
159
 
          strncpy(cpu_info->chip_type,"Unknown",8);
160
 
       }
161
 
       
162
 
          /* Opterons */
163
 
       if (strstr(model_string,"Opteron")!=NULL) {
164
 
          strncpy(cpu_info->chip_type,"Opteron",8);
165
 
       }
166
 
       
167
 
            
168
 
       
169
 
          /* Work around old kernels */
170
 
       if (model_string[0]==0) {
171
 
          if (plain_model[0]=='9') {
172
 
             strncpy(cpu_info->chip_type,"K6-III",7);
173
 
          }
174
 
       }
175
 
       
176
 
    }
177
 
   
178
 
       /* ***************** */
179
 
       /* Centaur Chips     */
180
 
       /* ***************** */
181
 
    if ( !(strncmp(vendor_string,"CentaurHauls",12))) {
182
 
                  /* centaur cpuid is fully customizable, but this */
183
 
                  /* should catch the common case                  */
184
 
       strncpy(cpu_info->chip_vendor,"Centaur",8);
185
 
       
186
 
          /* Is this the proper thing to do?  It looks silly */
187
 
          /* calling them "Centaur VIA" chips                */
188
 
       if (strstr(model_string,"VIA")!=NULL) {
189
 
          strncpy(cpu_info->chip_vendor,"VIA",4);
190
 
          
191
 
          strcpy(cpu_info->chip_type,model_string+4);
192
 
       }
193
 
    }
194
 
 
195
 
       /* *************** */
196
 
       /* Cyrix Chips     */
197
 
       /* *************** */
198
 
    if (!strncmp(vendor_string,"CyrixInstead",12)) {
199
 
       strncpy(cpu_info->chip_vendor,"Cyrix",6);
200
 
 
201
 
       if ( strstr(model_string,"MediaGX")!=NULL) 
202
 
          strncpy(cpu_info->chip_type,"MediaGX",8);
203
 
 
204
 
       if ( strstr(model_string,"Geode")!=NULL)
205
 
          strncpy(cpu_info->chip_type,"Geode",6);
206
 
       
207
 
       if ( !(strncmp(model_string,"6x86L",5)))
208
 
          strncpy(cpu_info->chip_type,"6x86",5);
209
 
       if ( !(strncmp(model_string,"6x86M",5)))
210
 
          strncpy(cpu_info->chip_type,"6x86MMX",8); 
211
 
       if ( !(strncmp(model_string,"M III",5))) 
212
 
          strncpy(cpu_info->chip_type,"MIII",5);
213
 
       else if ( !(strncmp(model_string,"M II",4))) 
214
 
          strncpy(cpu_info->chip_type,"MII",4);
215
 
    }
216
 
 
217
 
       /* *************** */
218
 
       /* Intel Chips     */
219
 
       /* *************** */
220
 
    if (!strncmp(vendor_string,"GenuineIntel",12)) {
221
 
       strncpy(cpu_info->chip_vendor,"Intel",6);
222
 
       
223
 
       if (!(strncmp(model_string,"Pentium",7))) {
224
 
          if (strstr(model_string,"75")!=NULL) {
225
 
             strncpy(cpu_info->chip_type,"Pentium",8);
226
 
          }
227
 
          if (strstr(model_string,"90")!=NULL) {
228
 
             strncpy(cpu_info->chip_type,"Pentium",8);  
229
 
          }
230
 
          if (strstr(model_string,"Pro")!=NULL) {
231
 
             strncpy(cpu_info->chip_type,"Pentium Pro",12);
232
 
          }
233
 
          if (strstr(model_string,"II")!=NULL) {
234
 
             strncpy(cpu_info->chip_type,"Pentium II",11);
235
 
          }
236
 
          if (strstr(model_string,"III")!=NULL) {
237
 
             strncpy(cpu_info->chip_type,"Pentium III",12);
238
 
          }
239
 
          if (strstr(model_string,"IV")!=NULL) {
240
 
             strncpy(cpu_info->chip_type,"Pentium IV",11);
241
 
          }
242
 
       }
243
 
       if (!(strncmp(model_string,"Intel(R) Pentium(R) 4",21))) {
244
 
          strncpy(cpu_info->chip_type,"Pentium 4",10);
245
 
       }
246
 
       
247
 
       if (strstr(model_string,"Pentium(R) M")!=NULL) {
248
 
          strncpy(cpu_info->chip_type,"Pentium M",10);
249
 
       }
250
 
       if (strstr(model_string,"Pentium(R) III")!=NULL) {
251
 
          strncpy(cpu_info->chip_type,"Pentium III",12);
252
 
       }
253
 
       if (strstr(model_string,"Xeon(TM) MP")!=NULL) {
254
 
          strncpy(cpu_info->chip_type,"Xeon MP",8);
255
 
       }
256
 
       if (strstr(model_string,"Pentium(R) 4 - M")!=NULL) {
257
 
          strncpy(cpu_info->chip_type,"Pentium 4 M",12);
258
 
       }
259
 
       
260
 
            
261
 
       
262
 
            
263
 
       
264
 
       if ( !(strncmp(model_string,"Intel(R) Xeon(TM) CPU",21)) ||
265
 
            !(strncmp(model_string,"Intel(R) Genuine CPU",19)) || 
266
 
            !(strncmp(model_string,"Intel(R) XEON(TM)",17))) {
267
 
          strncpy(cpu_info->chip_type,"Pentium 4 Xeon",16);
268
 
       }
269
 
            
270
 
       
271
 
          /* Should we handle all the various Celeron */
272
 
          /* types separately??                       */
273
 
       if (strstr(model_string,"Celeron")!=NULL) {
274
 
          strncpy(cpu_info->chip_type,"Celeron",8);
275
 
       }
276
 
       
277
 
       
278
 
          /* Fix up some older kernels */
279
 
       if (model_string[0]==0) {
280
 
          if (plain_model[0]=='5') {  
281
 
             strncpy(cpu_info->chip_type,"Pentium II",11);  
282
 
          }
283
 
       }
284
 
       if (!strncmp(model_string,"00/07",5)) {
285
 
          strncpy(cpu_info->chip_type,"Pentium III",12);
286
 
       }
287
 
    }
288
 
   
289
 
       /* ************* */
290
 
       /* NexGen        */
291
 
       /* ************* */
292
 
    if ( !(strncmp(vendor_string,"NexGenDriven",12))) {
293
 
       strncpy(cpu_info->chip_vendor,"NexGen",7);
294
 
    }
295
 
    
296
 
       /* ********************************** */
297
 
       /* National Semiconductor Geode Chips */
298
 
       /* ********************************** */
299
 
    if ( !(strncmp(vendor_string,"Geode by NSC",12))) {
300
 
       strncpy(cpu_info->chip_vendor,"NSC",4);
301
 
       strncpy(cpu_info->chip_type,"Geode",6);
302
 
    }
303
 
   
304
 
       /* ************* */
305
 
       /* Rise          */
306
 
       /* ************* */
307
 
    if ( !(strncmp(vendor_string,"RiseRiseRise",12))) {
308
 
       strncpy(cpu_info->chip_vendor,"Rise",5);
309
 
    }
310
 
   
311
 
       /* ************* */
312
 
       /* Transmeta     */
313
 
       /* ************* */
314
 
    if ( !(strncmp(vendor_string,"GenuineTMx86",12)) ||
315
 
         !(strncmp(vendor_string,"TransmetaCPU",12))) {
316
 
       strncpy(cpu_info->chip_vendor,"Transmeta",10); 
317
 
            
318
 
       if (strstr(model_string,"Crusoe")!=NULL) {
319
 
          strncpy(cpu_info->chip_type,"Crusoe",7);
320
 
       }
321
 
    }
322
 
   
323
 
       /* ************** */
324
 
       /* UMC            */
325
 
       /* ************** */
326
 
    if ( !(strncmp(vendor_string,"UMC UMC UMC",11))) {
327
 
       strncpy(cpu_info->chip_vendor,"UMC",4);
328
 
       
329
 
       if (!(strncmp(model_string,"SX",2))) {
330
 
          strncpy(cpu_info->chip_type,"486SX",6);
331
 
       }
332
 
    }
333
 
   
334
 
       /* ****************************** */
335
 
       /* Try to handle cpus w/o cpuinfo */
336
 
       /* ****************************** */
337
 
    if ( !(strncmp(cpu_info->chip_vendor,"Unknown",7))) {
338
 
       if (family!=0) {
339
 
          sprintf(temp_string,"%i86",family);
340
 
          strncpy(cpu_info->chip_type,temp_string,4);
341
 
       }
342
 
       else {
343
 
          if (plain_model!=NULL) { 
344
 
             strncpy(cpu_info->chip_type,plain_model,63);  
345
 
          }
346
 
       }
347
 
    }
348
 
   
349
 
    cpu_info->megahertz=0.0;
350
 
   
351
 
       /* Handle Pretty_printing */
352
 
    if (get_pretty_printing()) {
353
 
       /* Fix MHz */
354
 
       if (megahertz>0.0)
355
 
          cpu_info->megahertz=fix_megahertz(25,megahertz);
356
 
    }
357
 
    else {
358
 
       /* restore RAW vendor string.  Do we want this? */
359
 
       strncpy(cpu_info->chip_type,model_string,64);
360
 
       cpu_info->megahertz=megahertz;
361
 
    }
362
 
 
363
 
    return 0;   
364
 
}
365
 
 
366
 
    /* Not implemented on ix86 */
367
 
int get_hardware_info(char hardware_string[256]) {
368
 
   
369
 
    return 0;
370
 
}
371
 
 
372
 
    /* Some architectures might have better ways of detecting RAM size */
373
 
long int get_arch_specific_mem_size(void) {
374
 
    /* We have no special way of detecting RAM */
375
 
    return -1;
376
 
}