xosview  1.23
About: xosview is an X Windows based system monitor (cpu, memory, swap and network usage; interrupt and serial activities; load average).
  Fossies Dox: xosview-1.23.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

coretemp.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2008 by Tomi Tapper <tomi.o.tapper@jyu.fi>
3 //
4 // Read coretemp reading with sysctl and display actual temperature.
5 // If actual >= high, actual temp changes color to indicate alarm.
6 //
7 // File based on linux/lmstemp.* by
8 // Copyright (c) 2000, 2006 by Leopold Toetsch <lt@toetsch.at>
9 //
10 // This file may be distributed under terms of the GPL
11 //
12 //
13 //
14 
15 #include "coretemp.h"
16 #include "kernel.h"
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <iostream>
20 
21 
22 CoreTemp::CoreTemp( XOSView *parent, const char *label, const char *caption, int cpu)
23  : FieldMeter( parent, 3, label, caption, 1, 1, 1 ) {
24  metric_ = true;
25  cpu_ = cpu;
26  cpucount_ = countCpus();
27  temps_ = (float *)calloc(cpucount_, sizeof(float));
28 }
29 
31  free(temps_);
32 }
33 
36 
37  actcolor_ = parent_->allocColor( parent_->getResource( "coretempActColor" ) );
38  highcolor_ = parent_->allocColor( parent_->getResource( "coretempHighColor" ) );
40  setfieldcolor( 1, parent_->getResource( "coretempIdleColor") );
42 
43  priority_ = atoi( parent_->getResource( "coretempPriority" ) );
44  const char *highest = parent_->getResourceOrUseDefault( "coretempHighest", "100" );
45  total_ = atoi( highest );
46  const char *high = parent_->getResourceOrUseDefault( "coretempHigh", NULL );
47  SetUsedFormat( parent_->getResource( "coretempUsedFormat" ) );
48 
49  // Get tjMax here and use as total.
50  float total = -300.0;
51  float *tjmax = (float *)calloc(cpucount_, sizeof(float));
52  BSDGetCPUTemperature(temps_, tjmax);
53  for (int i = 0; i < cpucount_; i++) {
54  if (tjmax[i] > total)
55  total = tjmax[i];
56  }
57  free(tjmax);
58  if (total > 0.0)
59  total_ = total;
60 
61  char l[32];
62  if (!high) {
63  high_ = total_;
64  snprintf(l, 32, "ACT(\260C)/HIGH/%d", (int)total_);
65  }
66  else {
67  high_ = atoi( high );
68  snprintf(l, 32, "ACT(\260C)/%d/%d", (int)high_, (int)total_);
69  }
70  legend(l);
71 }
72 
73 unsigned int CoreTemp::countCpus( void ) {
74  return BSDGetCPUTemperature(NULL, NULL);
75 }
76 
77 void CoreTemp::checkevent( void ) {
78  getcoretemp();
79  drawfields();
80 }
81 
82 void CoreTemp::getcoretemp( void ) {
83  BSDGetCPUTemperature(temps_, NULL);
84 
85  fields_[0] = 0.0;
86  if ( cpu_ >= 0 && cpu_ < cpucount_ ) { // one core
87  fields_[0] = temps_[cpu_];
88  }
89  else if ( cpu_ == -1 ) { // average
90  float tempval = 0.0;
91  for (int i = 0; i < cpucount_; i++)
92  tempval += temps_[i];
93  fields_[0] = tempval / (float)cpucount_;
94  }
95  else if ( cpu_ == -2 ) { // maximum
96  float tempval = -300.0;
97  for (int i = 0; i < cpucount_; i++) {
98  if (temps_[i] > tempval)
99  tempval = temps_[i];
100  }
101  fields_[0] = tempval;
102  }
103  else { // should not happen
104  std::cerr << "Unknown CPU core number in coretemp." << std::endl;
105  parent_->done(1);
106  return;
107  }
108 
109  setUsed(fields_[0], total_);
110  if (fields_[0] < 0)
111  fields_[0] = 0;
112  fields_[1] = high_ - fields_[0];
113  fields_[2] = total_ - fields_[1] - fields_[0];
114  if (fields_[0] > total_)
115  fields_[0] = total_;
116  if (fields_[2] < 0)
117  fields_[2] = 0;
118 
119  if (fields_[1] < 0) { // alarm: T > high
120  fields_[1] = 0;
121  if (colors_[0] != highcolor_) {
123  drawlegend();
124  }
125  }
126  else {
127  if (colors_[0] != actcolor_) {
128  setfieldcolor( 0, actcolor_ );
129  drawlegend();
130  }
131  }
132 }
unsigned long actcolor_
Definition: coretemp.h:36
int cpu_
Definition: coretemp.h:34
void getcoretemp(void)
Definition: coretemp.cc:82
CoreTemp(XOSView *parent, const char *label, const char *caption, int cpu)
Definition: coretemp.cc:22
static unsigned int countCpus(void)
Definition: coretemp.cc:73
float high_
Definition: coretemp.h:35
float * temps_
Definition: coretemp.h:35
void checkevent(void)
Definition: coretemp.cc:77
unsigned long highcolor_
Definition: coretemp.h:36
int cpucount_
Definition: coretemp.h:34
~CoreTemp(void)
Definition: coretemp.cc:30
void checkResources(void)
Definition: coretemp.cc:34
double total_
Definition: fieldmeter.h:41
virtual void checkResources(void)
Definition: fieldmeter.cc:54
void setfieldcolor(int field, const char *color)
Definition: fieldmeter.cc:108
bool metric_
Definition: fieldmeter.h:48
void drawlegend(void)
Definition: fieldmeter.cc:136
void setUsed(double val, double total)
Definition: fieldmeter.cc:76
virtual void drawfields(int mandatory=0)
Definition: fieldmeter.cc:221
double * fields_
Definition: fieldmeter.h:40
unsigned long * colors_
Definition: fieldmeter.h:43
void SetUsedFormat(const char *const str)
Definition: fieldmeter.cc:60
int priority_
Definition: meter.h:54
const char * legend(void)
Definition: meter.h:27
XOSView * parent_
Definition: meter.h:52
unsigned long allocColor(const char *name)
Definition: xwin.cc:383
const char * getResourceOrUseDefault(const char *name, const char *defaultVal)
Definition: xwin.cc:351
int done(void)
Definition: xwin.h:29
const char * getResource(const char *name)
Definition: xwin.cc:362