RoyLongbottom
Posts: 283
Joined: Fri Apr 12, 2013 9:27 am
Location: Essex, UK
Contact: Website

CPU Temperature Recorder

Tue Jun 18, 2013 4:04 pm

There are probably better ones around, but in case anyone is looking for something like this, a listing of my CPU Temperature Recorder C program is below. The program will be run by me in conjunction with my reliability testing benchmarks, some of which are simple variations of other benchmarks, described in:

http://www.roylongbottom.org.uk/Raspber ... hmarks.htm

I will use the program in conjunction with two or three LXTerminal windows. The compile and run commands are show on the listing, along with run time parameters that determine sampling frequency and overall running time.

Code: Select all

// Compile command -  cc RPiTemperature.c -lrt -o RPiTemperature
// Run     command -  ./RPiTemperature passes ppp, seconds sss
//                    for ppp samples at sss second intervals
// Default            ./RPiTemperature
//                    10 samples at 1 second intervals  

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

FILE     *outfile;
char    timeday[30];
double  theseSecs = 0.0;
double  startSecs = 0.0;
double  secs;
struct  timespec tp1;
int     passes  = 10;
int     seconds = 1;


void getTemperature();
void start_time();
void end_time();
void getSecs();
void local_time();


void main(int argc, char *argv[])
{
    int i;
    int param1;

    for (i=1; i<5; i=i+2)
    {
       if (argc > i)
       {
          switch (toupper(argv[i][0]))
          {
               case 'P':
                param1 = 0;
                if (argc > i+1)
                {
                   sscanf(argv[i+1],"%d", &param1);
                   if (param1 > 0) passes = param1;
                }
                break;

                case 'S':
                param1 = 0;
                if (argc > i+1)
                {
                   sscanf(argv[i+1],"%d", &param1);
                   if (param1 > 0) seconds = param1;
                }
                break;
          }
       }
    }
    outfile = fopen("RPiTemperatures.txt", "a+");
    if (outfile == NULL)
    {
        printf ("Cannot open results file \n\n");
        exit (0);
    }

    local_time();
    fprintf(outfile, " Temperature Measurement - Start at %s\n", timeday); 
    fprintf(stdout,  " Temperature Measurement - Start at %s\n", timeday);
    fprintf(outfile, "          Using %d samples at %d second intervals\n\n", passes, seconds);
    fprintf(stdout,  "          Using %d samples at %d second intervals\n\n", passes, seconds);
    fprintf(outfile, " Seconds\n");
    fprintf(stdout,  " Seconds\n");
               
    secs = 0.0;
    getTemperature();

    start_time();

    for (i=0; i<passes; i++)
    {
        sleep(seconds);
        end_time();
        getTemperature();
    }
    local_time();
    fprintf(outfile, "\n Temperature Measurement - End at   %s\n", timeday); 
    fprintf(stdout,  "\n Temperature Measurement - End at   %s\n", timeday);
    fprintf(stdout,  " Results also in RPiTemperatures.txt where temperatures will\n"
                     " be slightly different as separate function calls are used.\n\n");

    fclose(outfile);
    return;
}

void getTemperature()
{
    fprintf(outfile, " %6.1f  ", secs);
    fprintf(stdout,  " %6.1f  ", secs);
    fflush(outfile);
    fflush (stdout);
    int g = system("/opt/vc/bin/vcgencmd measure_temp >> RPiTemperatures.txt");
    int h = system("/opt/vc/bin/vcgencmd measure_temp");
    return;
}

void local_time()
{
    time_t t;

    t = time(NULL);
    sprintf(timeday, "%s", asctime(localtime(&t)));
    return;
}

void getSecs()
{
     clock_gettime(CLOCK_REALTIME, &tp1);

     theseSecs =  tp1.tv_sec + tp1.tv_nsec / 1e9;           
     return;
}

void start_time()
{
    getSecs();
    startSecs = theseSecs;
    return;
}

void end_time()
{
    getSecs();
    secs = theseSecs - startSecs;
    return;
}    
Below are results running the tests at the same time as a modified version of my OpenGL benchmark to run the most demanding test for 8 minutes (16 tests x 30 seconds). For both programs, results are displayed as the tests are running and saved in .txt log files (enables matching of running times). Whilst running, reported CPU Utilisation was less than 50%. I will run further tests logging temperatures with the OpenGL program and another benchmark running (if it works). The source and execution codes are in:

http://www.roylongbottom.org.uk/Raspber ... hmarks.zip

Code: Select all

 Temperature Measurement - Start at Tue Jun 18 16:11:36 2013

          Using 30 samples at 20 second intervals

 Seconds
    0.0  temp=51.4'C
   20.0  temp=56.2'C
   40.2  temp=58.4'C
   60.3  temp=61.1'C
   80.5  temp=61.6'C
  100.7  temp=61.6'C
  120.9  temp=62.7'C
  141.1  temp=62.7'C
  161.2  temp=62.7'C
  181.4  temp=63.8'C
  201.5  temp=63.8'C
  221.7  temp=64.3'C
  241.9  temp=63.2'C
  262.0  temp=64.3'C
  282.2  temp=64.8'C
  302.4  temp=64.8'C
  322.5  temp=65.4'C
  342.7  temp=64.3'C
  362.9  temp=65.9'C
  383.0  temp=64.8'C
  403.1  temp=64.8'C
  423.3  temp=65.9'C
  443.5  temp=65.4'C
  463.7  temp=65.9'C
  483.9  temp=65.9'C
  504.0  temp=59.5'C
  524.1  temp=57.3'C
  544.1  temp=56.2'C
  564.2  temp=55.1'C
  584.2  temp=55.1'C
  604.3  temp=55.1'C

 Temperature Measurement - End at   Tue Jun 18 16:21:41 2013


RoyLongbottom
Posts: 283
Joined: Fri Apr 12, 2013 9:27 am
Location: Essex, UK
Contact: Website

Re: CPU Temperature Recorder

Sat Oct 19, 2013 10:17 am

The following function has been added to my temperature recording program, called at the start and end. It identifies MHz and voltage settings (see example below), particularly to show when overclocking is used.

Code: Select all

void getMHz()
{
    int qz;
    char buffer[100];

    qz = system("grep -v ^# /boot/config.txt |grep -v ^$ > thizPiz.txt");
    FILE    *infile;
    infile = fopen("thizPiz.txt","r");
    if (infile == NULL)
    {
        printf (" MHz details not available\n\n");
        fprintf (outfile, " MHz details not available\n\n");
    }
    else
    {
        while (fgets( buffer, 100, infile ) != NULL)
        {
            printf(" %s", buffer);
            fprintf(outfile, " %s", buffer);
        }
        printf("\n"); 
        fprintf(outfile, "\n"); 
    }
    fclose (infile);
}

 ### Output ###

 arm_freq=800
 core_freq=250
 sdram_freq=400
 over_voltage=0

RoyLongbottom
Posts: 283
Joined: Fri Apr 12, 2013 9:27 am
Location: Essex, UK
Contact: Website

Re: CPU Temperature Recorder

Sat Oct 19, 2013 5:14 pm

ednl wrote:You might also include cpu load. There is no easy "get current cpu load as a percentage" command that I know of, but here is one way to do it in Bash I just came up with
Going back more than 20 years, I used to benchmark Unix systems using the vmstat command. This runs on RPi but I have only looked at CPU utilisation, so far. Following is the command to run vmstat at the same time as my OpenGL benchmark. This shows CPU utilisation around 50%.

Code: Select all

vmstat 10 second intervals, 20 samples
us - CPU utilisation user
sy - CPU utilisation system
id - CPU idle
wa - waiting for I/O

./OpenGL1Pi.bin & vmstat 10 20 > vmstats.txt

procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 2  0      0 254712  20012 106772    0    0     6     1 1085  385 17  3 80  0
 0  0      0 254804  20020 106772    0    0     0     6 1359  891 37  5 58  0
 0  0      0 254804  20028 106772    0    0     0     4 1340  866 36  4 60  0
 1  0      0 254804  20036 106772    0    0     0     6 1872 2698 34 10 56  0
 0  0      0 254804  20044 106772    0    0     0     3 1791 2470 35  8 57  0
 1  0      0 254804  20052 106772    0    0     0     7 1909 2746 43  9 49  0
 1  0      0 254804  20060 106772    0    0     0     3 1894 2736 38 11 50  0
 1  0      0 254804  20068 106772    0    0     0     4 1888 2726 38 11 51  0
 0  0      0 254804  20076 106772    0    0     0     3 1712 2222 36  9 55  0
 0  0      0 254804  20084 106772    0    0     0     5 1891 2711 41  9 51  0
 1  0      0 254804  20092 106772    0    0     0     3 1890 2721 44  7 49  0
 1  0      0 254680  20100 106772    0    0     0     3 1886 2714 39 10 51  1
 1  0      0 254680  20108 106772    0    0     0     3 1698 2176 36 11 53  0
 1  0      0 254680  20116 106772    0    0     0     3 1885 2692 40 10 49  0
 1  0      0 254680  20124 106772    0    0     0     3 1891 2732 41  8 51  0
 0  0      0 254680  20132 106772    0    0     0     3 1885 2713 38 10 51  0
 1  0      0 254680  20140 106772    0    0     0     3 1696 2162 37 10 53  0
 0  0      0 254680  20148 106772    0    0     0     4 1045  314  8  3 89  0
 0  0      0 254688  20156 106772    0    0     0     2  943   58  2  1 98  0
 0  0      0 254688  20164 106772    0    0     0     2  943   58  2  1 97  0


RoyLongbottom
Posts: 283
Joined: Fri Apr 12, 2013 9:27 am
Location: Essex, UK
Contact: Website

Re: CPU Temperature Recorder

Sun Oct 20, 2013 10:45 pm

ednl wrote:I'm sure that's essentially the same, both are: 1 - (time spent idle / time interval). I used 1 second, so not very accurate but fairly instantaneous.
Sometimes, but what about with disk activity. Then waiting for I/O is not classified as idle. See following example with disk plus CPU test where CPU utilisation is 100% and idle 0% then just disk program (similar speed) with idle 0%, CPU utilisation average 41% and WIO average 59%.

Code: Select all

 Read passes     1 x 4 Files x  164.00 MB in     0.70 minutes
 Read passes     2 x 4 Files x  164.00 MB in     1.39 minutes
 Read passes     3 x 4 Files x  164.00 MB in     2.09 minutes


procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 2  1      0 335212   9628  40872    0    0   481    92  386  191  4  3 90  2
 3  1      0 327848   9652  40912    0    0     1  5959 3403  936 65 35  0  0
 2  1      0 327756   9680  40936    0    0     1  6141 3460  891 65 35  0  0
 4  1      0 327632   9712  40952    0    0     1  5918 3389  881 65 35  0  0
 4  1      0 327632   9736  40968    0    0     1  5760 3331  861 67 33  0  0
 3  1      0 327508   9764  40976    0    0     0  5993 3413  890 66 34  0  0
 2  1      0 327508   9792  41000    0    0     1  5653 3297  851 67 33  0  0
 1  1      0 327508   9816  41000    0    0     0  5980 3409  897 63 37  0  0
 2  1      0 327508   9840  41008    0    0  6635  3450 4589  961 75 25  0  0
 2  1      0 327384   9856  41008    0    0 15923     9 6264 1057 83 17  0  0
 2  1      0 327384   9864  41008    0    0 15940     2 6266 1056 82 18  0  0
 1  1      0 327384   9880  41008    0    0 15923     8 6268 1068 81 19  0  0
 1  1      0 327384   9896  41008    0    0 15923     3 6258 1053 85 15  0  0
 2  1      0 327384   9904  41008    0    0 15949     2 6265 1057 85 15  0  0
 1  1      0 327384   9928  41012    0    0 15949     8 6277 1066 82 18  0  0
 1  1      0 327384   9936  41012    0    0 15957     2 6268 1057 87 13  0  0
 2  1      0 327260   9944  41012    0    0 15936     4 6267 1056 81 19  0  0


Disk Test Only

lxterminal -e ./burnindrive Minutes 2
vmstat 15 25 > vmstats22.txt

 File 1  164.00 MB written in   28.52 seconds 
 File 2  164.00 MB written in   28.18 seconds 
 File 3  164.00 MB written in   25.66 seconds 
 File 4  164.00 MB written in   26.63 seconds 

 Read passes     1 x 4 Files x  164.00 MB in     0.71 minutes
 Read passes     2 x 4 Files x  164.00 MB in     1.41 minutes
 Read passes     3 x 4 Files x  164.00 MB in     2.11 minutes

procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 2  0      0 337632   9080  40812    0    0   366    78  322  182  4  3 91  1
 1  2      0 336112   9100  40840    0    0     1  7245 3909 1067 17 45  0 38
 0  1      0 335988   9132  40864    0    0     1  7210 3907 1004 13 42  0 45
 0  1      0 335988   9156  40872    0    0     0  7563 4027 1045 11 45  0 43
 0  1      0 335864   9188  40896    0    0     1  7176 3892  999 13 44  0 43
 0  1      0 335864   9216  40920    0    0     1  7338 3942 1019 13 45  0 42
 0  1      0 335864   9244  40928    0    0     0  7462 3990 1034 12 46  0 42
 0  1      0 335740   9252  40928    0    0 14029   844 6139 1259 16 18  0 66
 1  0      0 335740   9260  40928    0    0 16124     1 6506 1310 14 13  0 73
 0  1      0 335740   9268  40928    0    0 16060     3 6489 1306 14 13  0 74
 1  0      0 335740   9276  40928    0    0 16094     3 6497 1307 15 12  0 72
 0  1      0 335740   9284  40928    0    0 16115     2 6503 1309 15 14  0 71
 1  0      0 335740   9292  40928    0    0 16073     1 6496 1306 16 15  0 69
 0  1      0 335740   9300  40928    0    0 16094     3 6501 1308 15 14  0 71
 1  0      0 335740   9308  40928    0    0 16119     2 6510 1309 15 14  0 71



RoyLongbottom
Posts: 283
Joined: Fri Apr 12, 2013 9:27 am
Location: Essex, UK
Contact: Website

Re: CPU Temperature Recorder

Sun Mar 01, 2015 5:54 pm

Raspberry Pi 2 Temperature and MHz Recorder

The replacement for my RPiTemperature program is RPiHeatMHz in folder Temperature_MHz_Test in:

http://www.roylongbottom.org.uk/Raspber ... hmarks.zip

This displays and logs temperature and CPU MHz. On idling, after running a program, the latter now reduces, after a few seconds. Frequencies and voltage settings, on booting, as previously logged, are now important, during overclocking and after, as it is currently easy to select the wrong frequency (i.e. 700 MHz) [RPi 2 Defaults arm_freq=900 core_freq=250 sdram_freq=450 To change sudo nano /boot/config.txt].

Following is log (and display) whilst running a benchmark.

Code: Select all

 ########################################
 With Dhrystone Benchmark duration 6 seconds
 At 900 MHz for 14 seconds

 command -  ./RPiHeatMHz passes 25, seconds 1

 ########################################

 Temperature and CPU MHz Measurement

 Start at Sun Mar  1 03:13:24 2015

 Using 25 samples at 1 second intervals

 Boot Settings

 arm_freq=900
 hdmi_force_hotplug=1
 config_hdmi_boost=4
 overscan_left=24
 overscan_right=24
 overscan_top=16
 overscan_bottom=16
 disable_overscan=0
 core_freq=250
 sdram_freq=450
 over_voltage=0

 Seconds
    0.0   600 MHz  temp=46.5'C
    1.0   600 MHz  temp=46.5'C
    2.1   600 MHz  temp=46.5'C
    3.2   600 MHz  temp=46.5'C
    4.3   600 MHz  temp=46.5'C
    5.3   600 MHz  temp=46.5'C
    6.4   900 MHz  temp=46.5'C
    7.5   900 MHz  temp=48.2'C
    8.5   900 MHz  temp=48.2'C
    9.6   900 MHz  temp=48.7'C
   10.7   900 MHz  temp=48.7'C
   11.7   900 MHz  temp=48.2'C
   12.8   900 MHz  temp=48.7'C
   13.8   900 MHz  temp=48.2'C
   14.9   900 MHz  temp=48.2'C
   15.9   900 MHz  temp=48.2'C
   17.0   900 MHz  temp=48.2'C
   18.0   900 MHz  temp=47.6'C
   19.1   900 MHz  temp=47.6'C
   20.2   900 MHz  temp=48.2'C
   21.2   600 MHz  temp=48.2'C
   22.3   600 MHz  temp=47.6'C
   23.4   600 MHz  temp=47.6'C
   24.5   600 MHz  temp=46.5'C
   25.5   600 MHz  temp=47.1'C

 End at   Sun Mar  1 03:13:50 2015


Return to “General programming discussion”