"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/bsd/coretemp.cc" (11 Jul 2020, 3301 Bytes) of package /linux/misc/xosview-1.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "coretemp.cc" see the Fossies "Dox" file reference documentation.

    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 
   30 CoreTemp::~CoreTemp( void ) {
   31     free(temps_);
   32 }
   33 
   34 void CoreTemp::checkResources( void ) {
   35     FieldMeter::checkResources();
   36 
   37     actcolor_  = parent_->allocColor( parent_->getResource( "coretempActColor" ) );
   38     highcolor_ = parent_->allocColor( parent_->getResource( "coretempHighColor" ) );
   39     setfieldcolor( 0, actcolor_ );
   40     setfieldcolor( 1, parent_->getResource( "coretempIdleColor") );
   41     setfieldcolor( 2, highcolor_ );
   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_) {
  122             setfieldcolor( 0, highcolor_ );
  123             drawlegend();
  124         }
  125     }
  126     else {
  127         if (colors_[0] != actcolor_) {
  128             setfieldcolor( 0, actcolor_ );
  129             drawlegend();
  130         }
  131     }
  132 }