aboutsummaryrefslogtreecommitdiff
path: root/src/ss/os.c
blob: 8543c5f68aeedbd641aa49243f805e02891ff182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include "zenmonitor.h"
#include "sysfs.h"
#include "os.h"

#define OS_FREQ_PRINTF_FORMAT " %8.3f GHz"

static gchar **frq_files = NULL;
static guint cores;
static struct cpudev *cpu_dev_ids;

gfloat *core_freq;
gfloat *core_freq_min;
gfloat *core_freq_max;

static gdouble get_frequency(guint corei) {
    gchar *data;
    gdouble freq;

    if (!g_file_get_contents(frq_files[corei], &data, NULL, NULL))
        return 0.0;

    freq = atoi(data) / 1000000.0;
    g_free(data);

    return freq;
}

gboolean os_init(void) {
    guint i;

    if (!check_zen())
        return FALSE;

    cores = get_core_count();
    if (cores == 0)
        return FALSE;

    cpu_dev_ids = get_cpu_dev_ids();
    frq_files = malloc(cores * sizeof (gchar*));
    for (i = 0; i < cores; i++) {
        frq_files[i] = g_strdup_printf(
                        "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq",
                        cpu_dev_ids[i].cpuid);
    }

    core_freq = malloc(cores * sizeof (gfloat));
    core_freq_min = malloc(cores * sizeof (gfloat));
    core_freq_max = malloc(cores * sizeof (gfloat));

    os_update();
    memcpy(core_freq_min, core_freq, cores * sizeof (gfloat));
    memcpy(core_freq_max, core_freq, cores * sizeof (gfloat));

    return TRUE;
}

void os_update(void) {
    guint i;

    for (i = 0; i < cores; i++) {
        core_freq[i] = get_frequency(i);
        if (core_freq[i] < core_freq_min[i])
            core_freq_min[i] = core_freq[i];
        if (core_freq[i] > core_freq_max[i])
            core_freq_max[i] = core_freq[i];
    }
}

void os_clear_minmax(void) {
    guint i;

    for (i = 0; i < cores; i++) {
        core_freq_min[i] = core_freq[i];
        core_freq_max[i] = core_freq[i];
    }
}

GSList* os_get_sensors(void) {
    GSList *list = NULL;
    SensorInit *data;
    guint i;

    for (i = 0; i < cores; i++) {
        data = sensor_init_new();
        data->label = g_strdup_printf("Core %d Frequency", display_coreid ? cpu_dev_ids[i].coreid: i);
        data->value = &(core_freq[i]);
        data->min = &(core_freq_min[i]);
        data->max = &(core_freq_max[i]);
        data->printf_format = OS_FREQ_PRINTF_FORMAT;
        list = g_slist_append(list, data);
    }

    return list;
}