sys-cpu 1.0.2 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,15 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'win32ole'
2
4
  require 'socket'
3
5
 
4
- # See Ruby bugs #2618 and #7681. This is a workaround.
5
- BEGIN{
6
- require 'win32ole'
7
- if RUBY_VERSION.to_f < 2.0
8
- WIN32OLE.ole_initialize
9
- at_exit { WIN32OLE.ole_uninitialize }
10
- end
11
- }
12
-
13
6
  # The Sys module serves only as a namespace
14
7
  module Sys
15
8
  # Encapsulates system CPU information
@@ -78,12 +71,12 @@ module Sys
78
71
  # Returns the +host+ CPU's architecture, or nil if it cannot be
79
72
  # determined.
80
73
  #
81
- def self.architecture(host=Socket.gethostname)
74
+ def self.architecture(host = Socket.gethostname)
82
75
  cs = BASE_CS + "//#{host}/root/cimv2:Win32_Processor='cpu0'"
83
76
  begin
84
77
  wmi = WIN32OLE.connect(cs)
85
- rescue WIN32OLERuntimeError => e
86
- raise Error, e
78
+ rescue WIN32OLERuntimeError => err
79
+ raise Error, err
87
80
  else
88
81
  get_cpu_arch(wmi.Architecture)
89
82
  end
@@ -98,8 +91,8 @@ module Sys
98
91
  cs = BASE_CS + "//#{host}/root/cimv2:Win32_Processor='cpu#{cpu_num}'"
99
92
  begin
100
93
  wmi = WIN32OLE.connect(cs)
101
- rescue WIN32OLERuntimeError => e
102
- raise Error, e
94
+ rescue WIN32OLERuntimeError => err
95
+ raise Error, err
103
96
  else
104
97
  wmi.CurrentClockSpeed
105
98
  end
@@ -117,8 +110,8 @@ module Sys
117
110
  cs = BASE_CS + "//#{host}/root/cimv2:Win32_Processor='cpu#{cpu_num}'"
118
111
  begin
119
112
  wmi = WIN32OLE.connect(cs)
120
- rescue WIN32OLERuntimeError => e
121
- raise Error, e
113
+ rescue WIN32OLERuntimeError => err
114
+ raise Error, err
122
115
  else
123
116
  wmi.LoadPercentage
124
117
  end
@@ -130,8 +123,8 @@ module Sys
130
123
  cs = BASE_CS + "//#{host}/root/cimv2:Win32_Processor='cpu0'"
131
124
  begin
132
125
  wmi = WIN32OLE.connect(cs)
133
- rescue WIN32OLERuntimeError => e
134
- raise Error, e
126
+ rescue WIN32OLERuntimeError => err
127
+ raise Error, err
135
128
  else
136
129
  wmi.Name
137
130
  end
@@ -145,8 +138,8 @@ module Sys
145
138
  cs = BASE_CS + "//#{host}/root/cimv2:Win32_ComputerSystem='#{host}'"
146
139
  begin
147
140
  wmi = WIN32OLE.connect(cs)
148
- rescue WIN32OLERuntimeError => e
149
- raise Error, e
141
+ rescue WIN32OLERuntimeError => err
142
+ raise Error, err
150
143
  else
151
144
  wmi.NumberOfProcessors
152
145
  end
@@ -201,14 +194,15 @@ module Sys
201
194
  # * voltage_caps
202
195
  #
203
196
  # Note that not all of these members will necessarily be defined.
204
- #
197
+ #--
198
+ # rubocop:disable Metrics/BlockLength
205
199
  def self.processors(host = Socket.gethostname) # :yields: CPUStruct
206
200
  begin
207
201
  wmi = WIN32OLE.connect(BASE_CS + "//#{host}/root/cimv2")
208
- rescue WIN32OLERuntimeError => e
209
- raise Error, e
202
+ rescue WIN32OLERuntimeError => err
203
+ raise Error, err
210
204
  else
211
- wmi.InstancesOf('Win32_Processor').each{ |cpu|
205
+ wmi.InstancesOf('Win32_Processor').each do |cpu|
212
206
  yield CPUStruct.new(
213
207
  cpu.AddressWidth,
214
208
  get_cpu_arch(cpu.Architecture),
@@ -241,7 +235,7 @@ module Sys
241
235
  cpu.PowerManagementSupported,
242
236
  cpu.PowerManagementCapabilities,
243
237
  cpu.ProcessorId,
244
- self.get_processor_type(cpu.ProcessorType),
238
+ get_processor_type(cpu.ProcessorType),
245
239
  cpu.Revision,
246
240
  cpu.Role,
247
241
  cpu.SocketDesignation,
@@ -251,22 +245,24 @@ module Sys
251
245
  cpu.SystemCreationClassName,
252
246
  cpu.SystemName,
253
247
  cpu.UniqueId,
254
- self.get_upgrade_method(cpu.UpgradeMethod),
248
+ get_upgrade_method(cpu.UpgradeMethod),
255
249
  cpu.Version,
256
- self.get_voltage_caps(cpu.VoltageCaps)
250
+ get_voltage_caps(cpu.VoltageCaps)
257
251
  )
258
- }
252
+ end
259
253
  end
260
254
  end
261
255
 
256
+ # rubocop:enable Metrics/BlockLength
257
+
262
258
  # Returns a string indicating the type of processor, e.g. GenuineIntel.
263
259
  #
264
260
  def self.cpu_type(host = Socket.gethostname)
265
261
  cs = BASE_CS + "//#{host}/root/cimv2:Win32_Processor='cpu0'"
266
262
  begin
267
263
  wmi = WIN32OLE.connect(cs)
268
- rescue WIN32OLERuntimeError => e
269
- raise Error, e
264
+ rescue WIN32OLERuntimeError => err
265
+ raise Error, err
270
266
  else
271
267
  wmi.Manufacturer
272
268
  end
@@ -280,14 +276,11 @@ module Sys
280
276
  def self.get_cmec(num)
281
277
  case num
282
278
  when 0
283
- str = 'The device is working properly.'
284
- str
279
+ 'The device is working properly.'
285
280
  when 1
286
- str = 'The device is not configured correctly.'
287
- str
281
+ 'The device is not configured correctly.'
288
282
  when 2
289
- str = 'Windows cannot load the driver for the device.'
290
- str
283
+ 'Windows cannot load the driver for the device.'
291
284
  when 3
292
285
  str = 'The driver for the device might be corrupted, or the'
293
286
  str << ' system may be running low on memory or other'
@@ -306,29 +299,24 @@ module Sys
306
299
  str << ' other devices.'
307
300
  str
308
301
  when 7
309
- str = 'Cannot filter.'
310
- str
302
+ 'Cannot filter.'
311
303
  when 8
312
- str = 'The driver loader for the device is missing.'
313
- str
304
+ 'The driver loader for the device is missing.'
314
305
  when 9
315
306
  str = 'This device is not working properly because the'
316
307
  str << ' controlling firmware is reporting the resources'
317
308
  str << ' for the device incorrectly.'
318
309
  str
319
310
  when 10
320
- str = 'This device cannot start.'
321
- str
311
+ 'This device cannot start.'
322
312
  when 11
323
- str = 'This device failed.'
324
- str
313
+ 'This device failed.'
325
314
  when 12
326
315
  str = 'This device cannot find enough free resources that'
327
316
  str << ' it can use.'
328
317
  str
329
318
  when 13
330
- str = "Windows cannot verify this device's resources."
331
- str
319
+ "Windows cannot verify this device's resources."
332
320
  when 14
333
321
  str = 'This device cannot work properly until you restart'
334
322
  str << ' your computer.'
@@ -338,29 +326,24 @@ module Sys
338
326
  str << ' probably a re-enumeration problem.'
339
327
  str
340
328
  when 16
341
- str = 'Windows cannot identify all the resources this device '
342
- str << ' uses.'
343
- str
344
- when 17
345
- str = 'This device is asking for an unknown resource type.'
329
+ str = 'Windows cannot identify all the resources this device '
330
+ str << ' uses.'
346
331
  str
332
+ when 17
333
+ 'This device is asking for an unknown resource type.'
347
334
  when 18
348
- str = 'Reinstall the drivers for this device.'
349
- str
335
+ 'Reinstall the drivers for this device.'
350
336
  when 19
351
- str = 'Failure using the VXD loader.'
352
- str
337
+ 'Failure using the VXD loader.'
353
338
  when 20
354
- str = 'Your registry might be corrupted.'
355
- str
339
+ 'Your registry might be corrupted.'
356
340
  when 21
357
341
  str = 'System failure: try changing the driver for this device.'
358
342
  str << ' If that does not work, see your hardware documentation.'
359
343
  str << ' Windows is removing this device.'
360
344
  str
361
345
  when 22
362
- str = 'This device is disabled.'
363
- str
346
+ 'This device is disabled.'
364
347
  when 23
365
348
  str = 'System failure: try changing the driver for this device.'
366
349
  str << "If that doesn't work, see your hardware documentation."
@@ -369,18 +352,12 @@ module Sys
369
352
  str = 'This device is not present, not working properly, or'
370
353
  str << ' does not have all its drivers installed.'
371
354
  str
372
- when 25
373
- str = 'Windows is still setting up this device.'
374
- str
375
- when 26
376
- str = 'Windows is still setting up this device.'
377
- str
355
+ when 25, 26
356
+ 'Windows is still setting up this device.'
378
357
  when 27
379
- str = 'This device does not have valid log configuration.'
380
- str
358
+ 'This device does not have valid log configuration.'
381
359
  when 28
382
- str = 'The drivers for this device are not installed.'
383
- str
360
+ 'The drivers for this device are not installed.'
384
361
  when 29
385
362
  str = 'This device is disabled because the firmware of the'
386
363
  str << ' device did not give it the required resources.'
@@ -393,8 +370,6 @@ module Sys
393
370
  str = 'This device is not working properly because Windows'
394
371
  str << ' cannot load the drivers required for this device'
395
372
  str
396
- else
397
- nil
398
373
  end
399
374
  end
400
375
 
@@ -415,8 +390,6 @@ module Sys
415
390
  'IA64'
416
391
  when 9
417
392
  'x64'
418
- else
419
- nil
420
393
  end
421
394
  end
422
395
 
@@ -467,8 +440,6 @@ module Sys
467
440
  'Not Configured'
468
441
  when 21
469
442
  'Quiesced'
470
- else
471
- nil
472
443
  end
473
444
  end
474
445
 
@@ -490,14 +461,16 @@ module Sys
490
461
  'Idle'
491
462
  when 7
492
463
  'Other'
493
- else
494
- nil
495
464
  end
496
465
  end
497
466
 
498
467
  private_class_method :get_status
499
468
 
500
469
  # Convert a family number into the equivalent string
470
+ #
471
+ # NOTE: This could be out of date as new data is added occasionally.
472
+ # If there's a nicer way to do this, please send a PR my way.
473
+ #
501
474
  def self.get_family(num)
502
475
  case num
503
476
  when 1
@@ -521,19 +494,19 @@ module Sys
521
494
  when 10
522
495
  '80487'
523
496
  when 11
524
- 'Pentium?'
497
+ 'Pentium'
525
498
  when 12
526
- 'Pentium?'
499
+ 'Pentium Pro'
527
500
  when 13
528
- 'Pentium?'
501
+ 'Pentium II'
529
502
  when 14
530
- 'Pentium?'
503
+ 'Pentium with MMX'
531
504
  when 15
532
- 'Celeron?'
505
+ 'Celeron'
533
506
  when 16
534
- 'Pentium?'
507
+ 'Pentium II Xeon'
535
508
  when 17
536
- 'Pentium?'
509
+ 'Pentium III'
537
510
  when 18
538
511
  'M1'
539
512
  when 19
@@ -547,75 +520,75 @@ module Sys
547
520
  when 27
548
521
  'K6-3'
549
522
  when 28
550
- 'AMD'
523
+ 'AMD Athlon'
551
524
  when 29
552
- 'AMD?'
525
+ 'AMD Duron'
553
526
  when 30
554
527
  'AMD2900'
555
528
  when 31
556
529
  'K6-2+'
557
530
  when 32
558
- 'Power'
531
+ 'Power PC'
559
532
  when 33
560
- 'Power'
533
+ 'Power 601'
561
534
  when 34
562
- 'Power'
535
+ 'Power 603'
563
536
  when 35
564
- 'Power'
537
+ 'Power 603+'
565
538
  when 36
566
- 'Power'
539
+ 'Power 604'
567
540
  when 37
568
- 'Power'
541
+ 'Power 620'
569
542
  when 38
570
- 'Power'
543
+ 'Power X704'
571
544
  when 39
572
- 'Power'
545
+ 'Power 750'
573
546
  when 48
574
547
  'Alpha'
575
548
  when 49
576
- 'Alpha'
549
+ 'Alpha 21064'
577
550
  when 50
578
- 'Alpha'
551
+ 'Alpha 21066'
579
552
  when 51
580
- 'Alpha'
553
+ 'Alpha 21164'
581
554
  when 52
582
- 'Alpha'
555
+ 'Alpha 21164PC'
583
556
  when 53
584
- 'Alpha'
557
+ 'Alpha 21164a'
585
558
  when 54
586
- 'Alpha'
559
+ 'Alpha 21264'
587
560
  when 55
588
- 'Alpha'
561
+ 'Alpha 21364'
589
562
  when 64
590
563
  'MIPS'
591
564
  when 65
592
- 'MIPS'
565
+ 'MIPS R4000'
593
566
  when 66
594
- 'MIPS'
567
+ 'MIPS R4200'
595
568
  when 67
596
- 'MIPS'
569
+ 'MIPS R4400'
597
570
  when 68
598
- 'MIPS'
571
+ 'MIPS R4600'
599
572
  when 69
600
- 'MIPS'
573
+ 'MIPS R10000'
601
574
  when 80
602
575
  'SPARC'
603
576
  when 81
604
577
  'SuperSPARC'
605
578
  when 82
606
- 'microSPARC'
579
+ 'microSPARC II'
607
580
  when 83
608
- 'microSPARC'
581
+ 'microSPARC IIep'
609
582
  when 84
610
583
  'UltraSPARC'
611
584
  when 85
612
- 'UltraSPARC'
585
+ 'UltraSPARC II'
613
586
  when 86
614
- 'UltraSPARC'
587
+ 'UltraSPARC IIi'
615
588
  when 87
616
- 'UltraSPARC'
589
+ 'UltraSPARC III'
617
590
  when 88
618
- 'UltraSPARC'
591
+ 'UltraSPARC IIIi'
619
592
  when 96
620
593
  '68040'
621
594
  when 97
@@ -631,57 +604,195 @@ module Sys
631
604
  when 112
632
605
  'Hobbit'
633
606
  when 120
634
- 'Crusoe?'
607
+ 'Crusoe TM5000'
635
608
  when 121
636
- 'Crusoe?'
609
+ 'Crusoe TM3000'
637
610
  when 128
638
611
  'Weitek'
639
612
  when 130
640
- 'Itanium?'
613
+ 'Itanium'
614
+ when 131
615
+ 'AMD Athlon 64'
616
+ when 132
617
+ 'AMD Opteron'
618
+ when 133
619
+ 'AMD Sempron'
620
+ when 134
621
+ 'AMD Turion 64 Mobile'
622
+ when 135
623
+ 'AMD Opteron Dual-Core'
624
+ when 136
625
+ 'AMD Athlon 64 X2 Dual-Core'
626
+ when 137
627
+ 'AMD Turion 64 X2 Mobile'
628
+ when 138
629
+ 'AMD Opteron Quad-Core'
630
+ when 139
631
+ 'AMD Opteron Third Generation'
632
+ when 140
633
+ 'AMD Phenom FX Quad-Core'
634
+ when 141
635
+ 'AMD Phenom X4 Quad-Core'
636
+ when 142
637
+ 'AMD Phenom X2 Dual-Core'
638
+ when 143
639
+ 'AMD Athlon X2 Dual-Core'
641
640
  when 144
642
641
  'PA-RISC'
643
642
  when 145
644
- 'PA-RISC'
643
+ 'PA-RISC 8500'
645
644
  when 146
646
- 'PA-RISC'
645
+ 'PA-RISC 8000'
647
646
  when 147
648
- 'PA-RISC'
647
+ 'PA-RISC 7300LC'
649
648
  when 148
650
- 'PA-RISC'
649
+ 'PA-RISC 7200'
651
650
  when 149
652
- 'PA-RISC'
651
+ 'PA-RISC 7100LC'
653
652
  when 150
654
- 'PA-RISC'
653
+ 'PA-RISC 7100'
655
654
  when 160
656
655
  'V30'
656
+ when 161
657
+ 'Intel Xeon 3200 Quad-Core'
658
+ when 162
659
+ 'Intel Xeon 3000 Dual-Core'
660
+ when 163
661
+ 'Intel Xeon 5300 Quad-Core'
662
+ when 164
663
+ 'Intel Xeon 5100 Dual-Core'
664
+ when 165
665
+ 'Intel Xeon 5000 Dual-Core'
666
+ when 166
667
+ 'Intel Xeon LV Dual-Core'
668
+ when 167
669
+ 'Intel Xeon ULV Dual-Core'
670
+ when 168
671
+ 'Intel Xeon 7100 Dual-Core'
672
+ when 169
673
+ 'Intel Xeon 5400 Quad-Core'
674
+ when 170
675
+ 'Intel Xeon Quad-Core'
676
+ when 171
677
+ 'Intel Xeon 5200 Dual-Core'
678
+ when 172
679
+ 'Intel Xeon 7200 Dual-Core'
680
+ when 173
681
+ 'Intel Xeon 7300 Quad-Core'
682
+ when 174
683
+ 'Intel Xeon 7400 Quad-Core'
684
+ when 175
685
+ 'Intel Xeon 7400 Multi-Core'
657
686
  when 176
658
- 'Pentium?'
687
+ 'Pentium III Xeon'
659
688
  when 177
660
- 'Pentium?'
689
+ 'Pentium III with SpeedStep'
661
690
  when 178
662
- 'Pentium?'
691
+ 'Pentium 4'
663
692
  when 179
664
- 'Intel?'
693
+ 'Intel Xeon'
665
694
  when 180
666
695
  'AS400'
667
696
  when 181
668
- 'Intel?'
697
+ 'Intel Xeon MP'
669
698
  when 182
670
- 'AMD'
699
+ 'AMD Athlon XP'
671
700
  when 183
672
- 'AMD'
701
+ 'AMD Athlon MP'
673
702
  when 184
674
- 'Intel?'
703
+ 'Intel Itanium 2'
675
704
  when 185
676
- 'AMD'
705
+ 'Intel Pentium M'
706
+ when 186
707
+ 'Intel Celeron D'
708
+ when 187
709
+ 'Intel Pentium D'
710
+ when 188
711
+ 'Intel Pentium Extreme Edition'
712
+ when 189
713
+ 'Intel Core Solo'
677
714
  when 190
678
715
  'K7'
716
+ when 191
717
+ 'Intel Core2 Duo'
718
+ when 192
719
+ 'Intel Core2 Solo'
720
+ when 193
721
+ 'Intel Core2 Extreme'
722
+ when 194
723
+ 'Intel Core2 Quad'
724
+ when 195
725
+ 'Intel Core2 Extreme Mobile'
726
+ when 196
727
+ 'Intel Core2 Duo Mobile'
728
+ when 197
729
+ 'Intel Core2 Solo Mobile'
730
+ when 198
731
+ 'Intel Core i7 Mobile'
732
+ when 199
733
+ 'Intel Celeron Dual-Core'
679
734
  when 200
680
- 'IBM390'
735
+ 'zSeries S/390'
681
736
  when 201
682
- 'G4'
737
+ 'ESA/390 G4'
683
738
  when 202
684
- 'G5'
739
+ 'ESA/390 G5'
740
+ when 203
741
+ 'ESA/390 G6'
742
+ when 204
743
+ 'z/Architectur'
744
+ when 205
745
+ 'Intel Core i5'
746
+ when 206
747
+ 'Intel Core i3'
748
+ when 210
749
+ 'VIA C7-M'
750
+ when 211
751
+ 'VIA C7-D'
752
+ when 212
753
+ 'VIA C7'
754
+ when 213
755
+ 'VIA Eden'
756
+ when 214
757
+ 'Intel Xeon Multi-Core'
758
+ when 215
759
+ 'Intel Xeon 3xxx Dual-Core'
760
+ when 216
761
+ 'Intel Xeon 3xxx Quad-Core'
762
+ when 217
763
+ 'VIA Nano'
764
+ when 218
765
+ 'Intel Xeon 5xxx Dual-Core'
766
+ when 219
767
+ 'Intel Xeon 5xxx Quad-Core'
768
+ when 221
769
+ 'Intel Xeon 7xxx Dual-Core'
770
+ when 222
771
+ 'Intel Xeon 7xxx Quad-Core'
772
+ when 223
773
+ 'Intel Xeon 7xxx Multi-Core'
774
+ when 224
775
+ 'Intel Xeon 3400 Multi-Core'
776
+ when 230
777
+ 'AMD Opteron Embedded Quad-Core'
778
+ when 231
779
+ 'AMD Phenom Triple-Core'
780
+ when 232
781
+ 'AMD Turion Ultra Dual-Core Mobile'
782
+ when 233
783
+ 'AMD Turion Dual-Core Mobile'
784
+ when 234
785
+ 'AMD Athlon Dual-Core'
786
+ when 235
787
+ 'AMD Sempron SI'
788
+ when 236
789
+ 'AMD Phenom II'
790
+ when 237
791
+ 'AMD Athlon II'
792
+ when 238
793
+ 'AMD Opteron Six-Core'
794
+ when 239
795
+ 'AMD Sempron M'
685
796
  when 250
686
797
  'i860'
687
798
  when 251
@@ -706,8 +817,6 @@ module Sys
706
817
  'DSP'
707
818
  when 500
708
819
  'Video'
709
- else
710
- nil
711
820
  end
712
821
  end
713
822
 
@@ -732,8 +841,6 @@ module Sys
732
841
  'Power Cycling Supported'
733
842
  when 7
734
843
  'Timed Power On Supported'
735
- else
736
- nil
737
844
  end
738
845
  end
739
846
 
@@ -754,8 +861,6 @@ module Sys
754
861
  'DSP Processor'
755
862
  when 6
756
863
  'Video Processor'
757
- else
758
- nil
759
864
  end
760
865
  end
761
866
 
@@ -788,8 +893,6 @@ module Sys
788
893
  'Slot A'
789
894
  when 12
790
895
  'Slot M'
791
- else
792
- nil
793
896
  end
794
897
  end
795
898
 
@@ -804,8 +907,6 @@ module Sys
804
907
  3.3
805
908
  when 4
806
909
  2.9
807
- else
808
- nil
809
910
  end
810
911
  end
811
912
 
data/lib/sys-cpu.rb CHANGED
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'sys/cpu'
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rspec'
4
+ require 'sys_cpu_shared'
2
5
 
3
6
  RSpec.configure do |config|
7
+ config.include_context(Sys::CPU)
4
8
  config.filter_run_excluding(:bsd) if RbConfig::CONFIG['host_os'] !~ /bsd|darwin|mach|osx/i
5
9
  config.filter_run_excluding(:sunos) if RbConfig::CONFIG['host_os'] !~ /sunos|solaris/i
6
10
  config.filter_run_excluding(:windows) if RbConfig::CONFIG['host_os'] !~ /mswin|win32|dos|mingw|cygwin/i