Class: Sys::CPU

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/cp/windows/sys/cpu.rb,
lib/cp/cpu.rb,
lib/cp/unix/sys/cpu.rb

Overview

Encapsulates system CPU information

Defined Under Namespace

Classes: CPUStruct, Error, ProcInfo

Constant Summary collapse

VERSION =

The version of the sys-cpu gem.

'0.7.2'
CTL_HW =

Generic hardware/cpu

6
HW_MACHINE =

Machine class

1
HW_MODEL =

Specific machine model

2
HW_NCPU =

Number of CPU’s

3
HW_CPU_FREQ =

CPU frequency

15
HW_MACHINE_ARCH =

Machine architecture

12
SI_MACHINE =
5
SI_ARCHITECTURE =
6
SC_NPROCESSORS_ONLN =
15
P_OFFLINE =
1
P_ONLINE =
2
P_FAULTED =
4
P_POWEROFF =
5
P_NOINTR =
6
P_SPARE =
7
CPU_ARCH_ABI64 =
0x01000000
CPU_TYPE_X86 =
7
CPU_TYPE_X86_64 =
(CPU_TYPE_X86 | CPU_ARCH_ABI64)
CPU_TYPE_SPARC =
14
CPU_TYPE_POWERPC =
18
CPU_TYPE_POWERPC64 =
CPU_TYPE_POWERPC | CPU_ARCH_ABI64

Class Method Summary collapse

Class Method Details

.architecture(host = Socket.gethostname) ⇒ Object

Returns the host CPU’s architecture, or nil if it cannot be determined.



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
# File 'lib/cp/unix/sys/cpu.rb', line 93

def self.architecture
  if respond_to?(:sysinfo, true)
    buf = 0.chr * 257

    if sysinfo(SI_ARCHITECTURE, buf, buf.size) < 0
      raise Error, "sysinfo function failed"
    end

    buf.strip
  elsif respond_to?(:sysctlbyname, true)
    optr = FFI::MemoryPointer.new(:char, 256)
    size = FFI::MemoryPointer.new(:size_t)

    size.write_int(optr.size)

    if RbConfig::CONFIG['host_os'] =~ /darwin/i
      name = 'hw.machine'
    else
      name = 'hw.machine_arch'
    end

    if sysctlbyname(name, optr, size, nil, 0) < 0
      raise Error, "sysctlbyname function failed"
    end

    optr.read_string
  else
    buf  = 0.chr * 64
    mib  = FFI::MemoryPointer.new(:int, 2)
    size = FFI::MemoryPointer.new(:long, 1)

    mib.write_array_of_int([CTL_HW, HW_MACHINE_ARCH])
    size.write_int(buf.size)

    if sysctl(mib, 2, buf, size, nil, 0) < 0
      raise Error, "sysctl function failed"
    end

    buf.strip
  end
end

.cpu_type(host = Socket.gethostname) ⇒ Object

Returns a string indicating the type of processor, e.g. GenuineIntel.



264
265
266
267
268
269
270
271
272
273
# File 'lib/cp/windows/sys/cpu.rb', line 264

def self.cpu_type(host = Socket.gethostname)
  cs = BASE_CS + "//#{host}/root/cimv2:Win32_Processor='cpu0'"
  begin
    wmi = WIN32OLE.connect(cs)
  rescue WIN32OLERuntimeError => e
    raise Error, e
  else
    return wmi.Manufacturer
  end
end

.fpu_typeObject

Returns the floating point processor type.

Not supported on all platforms.

Raises:

  • (NoMethodError)


326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/cp/unix/sys/cpu.rb', line 326

def self.fpu_type
  raise NoMethodError unless respond_to?(:processor_info, true)

  pinfo = ProcInfo.new

  if processor_info(0, pinfo) < 0
    if processor_info(1, pinfo) < 0
      raise Error, "process_info function failed"
    end
  end

  pinfo[:pi_fputypes].to_s
end

.freq(cpu_num = 0, host = Socket.gethostname) ⇒ Object

Returns an integer indicating the speed (i.e. frequency in Mhz) of cpu_num on host, or the localhost if no host is specified. If cpu_num +1 is greater than the number of cpu’s on your system or this call fails for any other reason, a Error is raised.



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
# File 'lib/cp/unix/sys/cpu.rb', line 258

def self.freq
  if respond_to?(:sysctlbyname, true)
    optr = FFI::MemoryPointer.new(:long)
    size = FFI::MemoryPointer.new(:size_t)

    size.write_long(optr.size)

    if RbConfig::CONFIG['host_os'] =~ /bsd/i
      name = 'hw.clockrate'
    else
      name = 'hw.cpufrequency'
    end

    if sysctlbyname(name, optr, size, nil, 0) < 0
      raise Error, "sysctlbyname failed"
    end

    if RbConfig::CONFIG['host_os'] =~ /darwin/i
      optr.read_long / 1000000
    else
      optr.read_long
    end
  elsif respond_to?(:sysctl, true)
    buf  = 0.chr * 16
    mib  = FFI::MemoryPointer.new(:int, 2)
    size = FFI::MemoryPointer.new(:long, 1)

    mib.write_array_of_int([CTL_HW, HW_CPU_FREQ])
    size.write_int(buf.size)

    if sysctl(mib, 2, buf, size, nil, 0) < 0
      raise Error, "sysctl function failed"
    end

    buf.unpack("I*").first / 1000000
  else
    pinfo = ProcInfo.new

    # Some systems start at 0, some at 1
    if processor_info(0, pinfo) < 0
      if processor_info(1, pinfo) < 0
        raise Error, "process_info function failed"
      end
    end

    pinfo[:pi_clock].to_i
  end
end

.get_availability(num) ⇒ Object

convert an Availability number into a string



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
# File 'lib/cp/windows/sys/cpu.rb', line 422

def self.get_availability(num)
  case num
    when 1
      return "Other"
    when 2
      return "Unknown"
    when 3
      return "Running"
    when 4
      return "Warning"
    when 5
      return "In Test"
    when 6
      return "Not Applicable"
    when 7
      return "Power Off"
    when 8
      return "Off Line"
    when 9
      return "Off Duty"
    when 10
      return "Degraded"
    when 11
      return "Not Installed"
    when 12
      return "Install Error"
    when 13
      return "Power Save - Unknown"
    when 14
      return "Power Save - Low Power Mode"
    when 15
      return "Power Save - Standby"
    when 16
      return "Power Cycle"
    when 17
      return "Power Save - Warning"
    when 18
      return "Paused"
    when 19
      return "Not Ready"
    when 20
      return "Not Configured"
    when 21
      return "Quiesced"
    else
      return nil
  end
end

.get_cmec(num) ⇒ Object

Convert the ConfigManagerErrorCode number to its corresponding string Note that this value returns nil on my system.



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
# File 'lib/cp/windows/sys/cpu.rb', line 280

def self.get_cmec(num)
  case
    when 0
      str = "The device is working properly."
      return str
    when 1
      str = "The device is not configured correctly."
      return str
    when 2
      str = "Windows cannot load the driver for the device."
      return str
    when 3
      str = "The driver for the device might be corrupted, or the"
      str << " system may be running low on memory or other"
      str << " resources."
      return str
    when 4
      str = "The device is not working properly. One of the drivers"
      str << " or the registry might be corrupted."
      return str
    when 5
      str = "The driver for this device needs a resource that"
      str << " Windows cannot manage."
      return str
    when 6
      str = "The boot configuration for this device conflicts with"
      str << " other devices."
      return str
    when 7
      str = "Cannot filter."
      return str
    when 8
      str = "The driver loader for the device is missing."
      return str
    when 9
      str = "This device is not working properly because the"
      str << " controlling firmware is reporting the resources"
      str << " for the device incorrectly."
      return str
    when 10
      str = "This device cannot start."
      return str
    when 11
      str = "This device failed."
      return str
    when 12
      str = "This device cannot find enough free resources that"
      str << " it can use."
      return str
    when 13
      str = "Windows cannot verify this device's resources."
      return str
    when 14
      str = "This device cannot work properly until you restart"
      str << " your computer."
      return str
    when 15
      str = "This device is not working properly because there is"
      str << " probably a re-enumeration problem."
      return str
    when 16
       str = "Windows cannot identify all the resources this device "
       str << " uses."
       return str
    when 17
      str = "This device is asking for an unknown resource type."
      return str
    when 18
      str = "Reinstall the drivers for this device."
      return str
    when 19
      str = "Failure using the VXD loader."
      return str
    when 20
      str = "Your registry might be corrupted."
      return str
    when 21
      str = "System failure: try changing the driver for this device."
      str << " If that does not work, see your hardware documentation."
      str << " Windows is removing this device."
      return str
    when 22
      str = "This device is disabled."
      return str
    when 23
      str = "System failure: try changing the driver for this device."
      str << "If that doesn't work, see your hardware documentation."
      return str
    when 24
      str = "This device is not present, not working properly, or"
      str << " does not have all its drivers installed."
      return str
    when 25
      str = "Windows is still setting up this device."
      return str
    when 26
      str = "Windows is still setting up this device."
      return str
    when 27
      str = "This device does not have valid log configuration."
      return str
    when 28
      str = "The drivers for this device are not installed."
      return str
    when 29
      str = "This device is disabled because the firmware of the"
      str << " device did not give it the required resources."
      return str
    when 30
      str = "This device is using an Interrupt Request (IRQ)"
      str << " resource that another device is using."
      return str
    when 31
      str = "This device is not working properly because Windows"
      str << " cannot load the drivers required for this device"
      return str
    else
      return nil
  end
end

.get_cpu_arch(num) ⇒ Object

Convert an cpu architecture number to a string



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/cp/windows/sys/cpu.rb', line 402

def self.get_cpu_arch(num)
  case num
    when 0
      return "x86"
    when 1
      return "MIPS"
    when 2
      return "Alpha"
    when 3
      return "PowerPC"
    when 6
      return "IA64"
    when 9
      return "x64"
    else
      return nil
  end
end

.get_family(num) ⇒ Object

Convert a family number into the equivalent string



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
# File 'lib/cp/windows/sys/cpu.rb', line 493

def self.get_family(num)
  case num
    when 1
      return "Other"
    when 2
      return "Unknown"
    when 3
      return "8086"
    when 4
      return "80286"
    when 5
      return "80386"
    when 6
      return "80486"
    when 7
      return "8087"
    when 8
      return "80287"
    when 9
      return "80387"
    when 10
      return "80487"
    when 11
      return "Pentium?"
    when 12
      return "Pentium?"
    when 13
      return "Pentium?"
    when 14
      return "Pentium?"
    when 15
      return "Celeron?"
    when 16
      return "Pentium?"
    when 17
      return "Pentium?"
    when 18
      return "M1"
    when 19
      return "M2"
    when 24
      return "K5"
    when 25
      return "K6"
    when 26
      return "K6-2"
    when 27
      return "K6-3"
    when 28
      return "AMD"
    when 29
      return "AMD?"
    when 30
      return "AMD2900"
    when 31
      return "K6-2+"
    when 32
      return "Power"
    when 33
      return "Power"
    when 34
      return "Power"
    when 35
      return "Power"
    when 36
      return "Power"
    when 37
      return "Power"
    when 38
      return "Power"
    when 39
      return "Power"
    when 48
      return "Alpha"
    when 49
      return "Alpha"
    when 50
      return "Alpha"
    when 51
      return "Alpha"
    when 52
      return "Alpha"
    when 53
      return "Alpha"
    when 54
      return "Alpha"
    when 55
      return "Alpha"
    when 64
      return "MIPS"
    when 65
      return "MIPS"
    when 66
      return "MIPS"
    when 67
      return "MIPS"
    when 68
      return "MIPS"
    when 69
      return "MIPS"
    when 80
      return "SPARC"
    when 81
      return "SuperSPARC"
    when 82
      return "microSPARC"
    when 83
      return "microSPARC"
    when 84
      return "UltraSPARC"
    when 85
      return "UltraSPARC"
    when 86
      return "UltraSPARC"
    when 87
      return "UltraSPARC"
    when 88
      return "UltraSPARC"
    when 96
      return "68040"
    when 97
      return "68xxx"
    when 98
      return "68000"
    when 99
      return "68010"
    when 100
      return "68020"
    when 101
      return "68030"
    when 112
      return "Hobbit"
    when 120
      return "Crusoe?"
    when 121
      return "Crusoe?"
    when 128
      return "Weitek"
    when 130
      return "Itanium?"
    when 144
      return "PA-RISC"
    when 145
      return "PA-RISC"
    when 146
      return "PA-RISC"
    when 147
      return "PA-RISC"
    when 148
      return "PA-RISC"
    when 149
      return "PA-RISC"
    when 150
      return "PA-RISC"
    when 160
      return "V30"
    when 176
      return "Pentium?"
    when 177
      return "Pentium?"
    when 178
      return "Pentium?"
    when 179
      return "Intel?"
    when 180
      return "AS400"
    when 181
      return "Intel?"
    when 182
      return "AMD"
    when 183
      return "AMD"
    when 184
      return "Intel?"
    when 185
      return "AMD"
    when 190
      return "K7"
    when 200
      return "IBM390"
    when 201
      return "G4"
    when 202
      return "G5"
    when 250
      return "i860"
    when 251
      return "i960"
    when 260
      return "SH-3"
    when 261
      return "SH-4"
    when 280
      return "ARM"
    when 281
      return "StrongARM"
    when 300
      return "6x86"
    when 301
      return "MediaGX"
    when 302
      return "MII"
    when 320
      return "WinChip"
    when 350
      return "DSP"
    when 500
      return "Video"
    else
      return nil
  end
end

.get_pmc(num) ⇒ Object

Convert power management capabilities number to its equivalent string



707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/cp/windows/sys/cpu.rb', line 707

def self.get_pmc(num)
  case num
    when 0
      return "Unknown"
    when 1
      return "Not Supported"
    when 2
      return "Disabled"
    when 3
      return "Enabled"
    when 4
      return "Power Saving Modes Entered Automatically"
    when 5
      return "Power State Settable"
    when 6
      return "Power Cycling Supported"
    when 7
      return "Timed Power On Supported"
    else
      return nil
  end
end

.get_processor_type(num) ⇒ Object

Convert a processor type into its equivalent string



731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
# File 'lib/cp/windows/sys/cpu.rb', line 731

def self.get_processor_type(num)
  case num
    when 1
      return "Other"
    when 2
      return "Unknown"
    when 3
      return "Central Processor"
    when 4
      return "Math Processor"
    when 5
      return "DSP Processor"
    when 6
      return "Video Processor"
    else
      return nil
  end
end

.get_status(num) ⇒ Object

convert CpuStatus to a string form. Note that values 5 and 6 are skipped because they’re reserved.



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/cp/windows/sys/cpu.rb', line 473

def self.get_status(num)
  case num
    when 0
      return "Unknown"
    when 1
      return "Enabled"
    when 2
      return "Disabled by User via BIOS Setup"
    when 3
      return "Disabled By BIOS (POST Error)"
    when 4
      return "Idle"
    when 7
      return "Other"
    else
      return nil
  end
end

.get_upgrade_method(num) ⇒ Object

Convert an upgrade method into its equivalent string



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
# File 'lib/cp/windows/sys/cpu.rb', line 751

def self.get_upgrade_method(num)
  case num
    when 1
      return "Other"
    when 2
      return "Unknown"
    when 3
      return "Daughter Board"
    when 4
      return "ZIF Socket"
    when 5
      return "Replacement/Piggy Back"
    when 6
      return "None"
    when 7
      return "LIF Socket"
    when 8
      return "Slot 1"
    when 9
      return "Slot 2"
    when 10
      return "370 Pin Socket"
    when 11
      return "Slot A"
    when 12
      return "Slot M"
    else
      return nil
  end
end

.get_voltage_caps(num) ⇒ Object

Convert return values to voltage cap values (floats)



783
784
785
786
787
788
789
790
791
792
793
794
# File 'lib/cp/windows/sys/cpu.rb', line 783

def self.get_voltage_caps(num)
  case num
    when 1
      return 5.0
    when 2
      return 3.3
    when 4
      return 2.9
    else
      return nil
  end
end

.load_avg(cpu_num = 0, host = Socket.gethostname) ⇒ Object

Returns the load capacity for cpu_num on host, or the localhost if no host is specified, averaged to the last second. Processor loading refers to the total computing burden for each processor at one time.

Note that this attribute is actually the LoadPercentage. I may use one of the Win32_Perf* classes in the future.



310
311
312
313
314
315
316
317
318
319
320
# File 'lib/cp/unix/sys/cpu.rb', line 310

def self.load_avg
  if respond_to?(:getloadavg, true)
    loadavg = FFI::MemoryPointer.new(:double, 3)

    if getloadavg(loadavg, loadavg.size) < 0
      raise Error, "getloadavg function failed"
    end

    loadavg.get_array_of_double(0, 3)
  end
end

.machineObject

Returns the cpu’s class type. On most systems this will be identical to the CPU.architecture method. On OpenBSD it will be identical to the CPU.model method.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/cp/unix/sys/cpu.rb', line 179

def self.machine
  if respond_to?(:sysctl, true)
    buf  = 0.chr * 32
    mib  = FFI::MemoryPointer.new(:int, 2)
    size = FFI::MemoryPointer.new(:long, 1)

    mib.write_array_of_int([CTL_HW, HW_MACHINE])
    size.write_int(buf.size)

    if sysctl(mib, 2, buf, size, nil, 0) < 0
      raise Error, "sysctl function failed"
    end

    buf.strip
  else
    buf = 0.chr * 257

    if sysinfo(SI_MACHINE, buf, buf.size) < 0
      raise Error, "sysinfo function failed"
    end

    buf.strip
  end
end

.model(host = Socket.gethostname) ⇒ Object

Returns a string indicating the cpu model, e.g. Intel Pentium 4.



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
# File 'lib/cp/unix/sys/cpu.rb', line 206

def self.model
  if RbConfig::CONFIG['host_os'] =~ /darwin/i
    ptr  = FFI::MemoryPointer.new(:long)
    size = FFI::MemoryPointer.new(:size_t)

    size.write_long(ptr.size)

    if sysctlbyname("hw.cputype", ptr, size, nil, 0) < 0
      raise "sysctlbyname function failed"
    end

    case ptr.read_long
      when  CPU_TYPE_X86, CPU_TYPE_X86_64
        "Intel"
      when CPU_TYPE_SPARC
        "Sparc"
      when CPU_TYPE_POWERPC, CPU_TYPE_POWERPC64
        "PowerPC"
      else
        "Unknown"
    end
  else
    if respond_to?(:sysctl, true)
      buf  = 0.chr * 64
      mib  = FFI::MemoryPointer.new(:int, 2)
      size = FFI::MemoryPointer.new(:long, 1)

      mib.write_array_of_int([CTL_HW, HW_MODEL])
      size.write_int(buf.size)

      if sysctl(mib, 2, buf, size, nil, 0) < 0
        raise Error, "sysctl function failed"
      end

      buf.strip
    else
      pinfo = ProcInfo.new

      # Some systems start at 0, some at 1
      if processor_info(0, pinfo) < 0
        if processor_info(1, pinfo) < 0
          raise Error, "process_info function failed"
        end
      end

      pinfo[:pi_processor_type].to_s
    end
  end
end

.num_cpu(host = Socket.gethostname) ⇒ Object

Returns an integer indicating the number of cpu’s on the system. – This (oddly) requires a different class.



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
# File 'lib/cp/unix/sys/cpu.rb', line 139

def self.num_cpu
  if respond_to?(:sysctlbyname, true)
    optr = FFI::MemoryPointer.new(:long)
    size = FFI::MemoryPointer.new(:size_t)

    size.write_long(optr.size)

    if sysctlbyname('hw.ncpu', optr, size, nil, 0) < 0
      raise Error, "sysctlbyname failed"
    end

    optr.read_long
  elsif respond_to?(:sysconf, true)
    num = sysconf(SC_NPROCESSORS_ONLN)

    if num < 0
      raise Error, "sysconf function failed"
    end

    num
  else
    buf  = 0.chr * 4
    mib  = FFI::MemoryPointer.new(:int, 2)
    size = FFI::MemoryPointer.new(:long, 1)

    mib.write_array_of_int([CTL_HW, HW_NCPU])
    size.write_int(buf.size)

    if sysctl(mib, 2, buf, size, nil, 0) < 0
      raise Error, "sysctl function failed"
    end

    buf.strip.unpack("C").first
  end
end

.processors(host = Socket.gethostname) ⇒ Object

Returns a CPUStruct for each CPU on host, or the localhost if no host is specified. A CPUStruct contains the following members:

  • address_width

  • architecture

  • availability

  • caption

  • config_manager_error_code

  • config_manager_user_config

  • cpu_status

  • creation_class_name

  • freq

  • voltage

  • data_width

  • description

  • device_id

  • error_cleared?

  • error_description

  • ext_clock

  • family

  • install_date

  • l2_cache_size

  • l2_cache_speed

  • last_error_code

  • level

  • load_avg

  • manufacturer

  • max_clock_speed

  • name

  • other_family_description

  • pnp_device_id

  • power_management_supported?

  • power_management_capabilities

  • processor_id

  • processor_type

  • revision

  • role

  • socket_designation

  • status

  • status_info

  • stepping

  • system_creation_class_name

  • system_name

  • unique_id

  • upgrade_method

  • version

  • voltage_caps

Note that not all of these members will necessarily be defined.



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
# File 'lib/cp/windows/sys/cpu.rb', line 205

def self.processors(host = Socket.gethostname) # :yields: CPUStruct
  begin
    wmi = WIN32OLE.connect(BASE_CS + "//#{host}/root/cimv2")
  rescue WIN32OLERuntimeError => e
    raise Error, e
  else
    wmi.InstancesOf("Win32_Processor").each{ |cpu|
      yield CPUStruct.new(
        cpu.AddressWidth,
        self.get_cpu_arch(cpu.Architecture),
        self.get_availability(cpu.Availability),
        cpu.Caption,
        self.get_cmec(cpu.ConfigManagerErrorCode),
        cpu.ConfigManagerUserConfig,
        get_status(cpu.CpuStatus),
        cpu.CreationClassName,
        cpu.CurrentClockSpeed,
        cpu.CurrentVoltage,
        cpu.DataWidth,
        cpu.Description,
        cpu.DeviceId,
        cpu.ErrorCleared,
        cpu.ErrorDescription,
        cpu.ExtClock,
        self.get_family(cpu.Family),
        cpu.InstallDate,
        cpu.L2CacheSize,
        cpu.L2CacheSpeed,
        cpu.LastErrorCode,
        cpu.Level,
        cpu.LoadPercentage,
        cpu.Manufacturer,
        cpu.MaxClockSpeed,
        cpu.Name,
        cpu.OtherFamilyDescription,
        cpu.PNPDeviceID,
        cpu.PowerManagementSupported,
        cpu.PowerManagementCapabilities,
        cpu.ProcessorId,
        self.get_processor_type(cpu.ProcessorType),
        cpu.Revision,
        cpu.Role,
        cpu.SocketDesignation,
        cpu.Status,
        cpu.StatusInfo,
        cpu.Stepping,
        cpu.SystemCreationClassName,
        cpu.SystemName,
        cpu.UniqueId,
        self.get_upgrade_method(cpu.UpgradeMethod),
        cpu.Version,
        self.get_voltage_caps(cpu.VoltageCaps)
      )
    }
  end
end

.state(num = 0) ⇒ Object

Returns the current state of processor num, or 0 if no number is specified.

Not supported on all platforms.

Raises:

  • (NoMethodError)


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
# File 'lib/cp/unix/sys/cpu.rb', line 345

def self.state(num = 0)
  raise NoMethodError unless respond_to?(:processor_info, true)

  pinfo = ProcInfo.new

  if processor_info(num, pinfo) < 0
    raise Error, "process_info function failed"
  end

  case pinfo[:pi_state].to_i
    when P_ONLINE
      "online"
    when P_OFFLINE
      "offline"
    when P_POWEROFF
      "poweroff"
    when P_FAULTED
      "faulted"
    when P_NOINTR
      "nointr"
    when P_SPARE
      "spare"
    else
      "unknown"
  end
end