Path: blob/master/tools/thermal/thermal-engine/thermal-engine.c
26282 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Thermal monitoring tool based on the thermal netlink events.3*4* Copyright (C) 2022 Linaro Ltd.5*6* Author: Daniel Lezcano <[email protected]>7*/8#include <errno.h>9#include <fcntl.h>10#include <getopt.h>11#include <libgen.h>12#include <limits.h>13#include <stdio.h>14#include <stdlib.h>15#include <string.h>16#include <signal.h>17#include <unistd.h>1819#include <syslog.h>2021#include <sys/epoll.h>22#include <sys/stat.h>23#include <sys/types.h>2425#include <thermal.h>26#include "thermal-tools.h"2728struct options {29int loglevel;30int logopt;31int interactive;32int daemonize;33};3435struct thermal_data {36struct thermal_zone *tz;37struct thermal_handler *th;38};3940static int show_threshold(struct thermal_threshold *th, __maybe_unused void *arg)41{42INFO("threshold temp=%d, direction=%d\n",43th->temperature, th->direction);4445return 0;46}4748static int show_trip(struct thermal_trip *tt, __maybe_unused void *arg)49{50INFO("trip id=%d, type=%d, temp=%d, hyst=%d\n",51tt->id, tt->type, tt->temp, tt->hyst);5253return 0;54}5556static int show_temp(struct thermal_zone *tz, __maybe_unused void *arg)57{58thermal_cmd_get_temp(arg, tz);5960INFO("temperature: %d\n", tz->temp);6162return 0;63}6465static int show_governor(struct thermal_zone *tz, __maybe_unused void *arg)66{67thermal_cmd_get_governor(arg, tz);6869INFO("governor: '%s'\n", tz->governor);7071return 0;72}7374static int show_tz(struct thermal_zone *tz, __maybe_unused void *arg)75{76INFO("thermal zone '%s', id=%d\n", tz->name, tz->id);7778for_each_thermal_trip(tz->trip, show_trip, NULL);7980for_each_thermal_threshold(tz->thresholds, show_threshold, NULL);8182show_temp(tz, arg);8384show_governor(tz, arg);8586return 0;87}8889static int set_threshold(struct thermal_zone *tz, __maybe_unused void *arg)90{91struct thermal_handler *th = arg;92int thresholds[] = { 43000, 65000, 49000, 55000, 57000 };93size_t i;9495INFO("Setting threshold for thermal zone '%s', id=%d\n", tz->name, tz->id);9697if (thermal_cmd_threshold_flush(th, tz)) {98ERROR("Failed to flush all previous thresholds\n");99return -1;100}101102for (i = 0; i < sizeof(thresholds) / sizeof(thresholds[0]); i++)103if (thermal_cmd_threshold_add(th, tz, thresholds[i],104THERMAL_THRESHOLD_WAY_UP |105THERMAL_THRESHOLD_WAY_DOWN)) {106ERROR("Failed to set threshold\n");107return -1;108}109110return 0;111}112113static int tz_create(const char *name, int tz_id, __maybe_unused void *arg)114{115INFO("Thermal zone '%s'/%d created\n", name, tz_id);116117return 0;118}119120static int tz_delete(int tz_id, __maybe_unused void *arg)121{122INFO("Thermal zone %d deleted\n", tz_id);123124return 0;125}126127static int tz_disable(int tz_id, void *arg)128{129struct thermal_data *td = arg;130struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);131132INFO("Thermal zone %d ('%s') disabled\n", tz_id, tz->name);133134return 0;135}136137static int tz_enable(int tz_id, void *arg)138{139struct thermal_data *td = arg;140struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);141142INFO("Thermal zone %d ('%s') enabled\n", tz_id, tz->name);143144return 0;145}146147static int trip_high(int tz_id, int trip_id, int temp, void *arg)148{149struct thermal_data *td = arg;150struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);151152INFO("Thermal zone %d ('%s'): trip point %d crossed way up with %d °C\n",153tz_id, tz->name, trip_id, temp);154155return 0;156}157158static int trip_low(int tz_id, int trip_id, int temp, void *arg)159{160struct thermal_data *td = arg;161struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);162163INFO("Thermal zone %d ('%s'): trip point %d crossed way down with %d °C\n",164tz_id, tz->name, trip_id, temp);165166return 0;167}168169static int trip_add(int tz_id, int trip_id, int type, int temp, int hyst, __maybe_unused void *arg)170{171INFO("Trip point added %d: id=%d, type=%d, temp=%d, hyst=%d\n",172tz_id, trip_id, type, temp, hyst);173174return 0;175}176177static int trip_delete(int tz_id, int trip_id, __maybe_unused void *arg)178{179INFO("Trip point deleted %d: id=%d\n", tz_id, trip_id);180181return 0;182}183184static int trip_change(int tz_id, int trip_id, int type, int temp,185int hyst, __maybe_unused void *arg)186{187struct thermal_data *td = arg;188struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);189190INFO("Trip point changed %d: id=%d, type=%d, temp=%d, hyst=%d\n",191tz_id, trip_id, type, temp, hyst);192193tz->trip[trip_id].type = type;194tz->trip[trip_id].temp = temp;195tz->trip[trip_id].hyst = hyst;196197return 0;198}199200static int cdev_add(const char *name, int cdev_id, int max_state, __maybe_unused void *arg)201{202INFO("Cooling device '%s'/%d (max state=%d) added\n", name, cdev_id, max_state);203204return 0;205}206207static int cdev_delete(int cdev_id, __maybe_unused void *arg)208{209INFO("Cooling device %d deleted", cdev_id);210211return 0;212}213214static int cdev_update(int cdev_id, int cur_state, __maybe_unused void *arg)215{216INFO("cdev:%d state:%d\n", cdev_id, cur_state);217218return 0;219}220221static int gov_change(int tz_id, const char *name, __maybe_unused void *arg)222{223struct thermal_data *td = arg;224struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);225226INFO("%s: governor changed %s -> %s\n", tz->name, tz->governor, name);227228strcpy(tz->governor, name);229230return 0;231}232233static int threshold_add(int tz_id, int temp, int direction, __maybe_unused void *arg)234{235INFO("Threshold added tz_id=%d: temp=%d, direction=%d\n", tz_id, temp, direction);236237return 0;238}239240static int threshold_delete(int tz_id, int temp, int direction, __maybe_unused void *arg)241{242INFO("Threshold deleted tz_id=%d: temp=%d, direction=%d\n", tz_id, temp, direction);243244return 0;245}246247static int threshold_flush(int tz_id, __maybe_unused void *arg)248{249INFO("Thresholds flushed tz_id=%d\n", tz_id);250251return 0;252}253254static int threshold_up(int tz_id, int temp, int prev_temp, __maybe_unused void *arg)255{256INFO("Threshold crossed way up tz_id=%d: temp=%d, prev_temp=%d\n",257tz_id, temp, prev_temp);258259return 0;260}261262static int threshold_down(int tz_id, int temp, int prev_temp, __maybe_unused void *arg)263{264INFO("Threshold crossed way down tz_id=%d: temp=%d, prev_temp=%d\n",265tz_id, temp, prev_temp);266267return 0;268}269270static struct thermal_ops ops = {271.events.tz_create = tz_create,272.events.tz_delete = tz_delete,273.events.tz_disable = tz_disable,274.events.tz_enable = tz_enable,275.events.trip_high = trip_high,276.events.trip_low = trip_low,277.events.trip_add = trip_add,278.events.trip_delete = trip_delete,279.events.trip_change = trip_change,280.events.cdev_add = cdev_add,281.events.cdev_delete = cdev_delete,282.events.cdev_update = cdev_update,283.events.gov_change = gov_change,284.events.threshold_add = threshold_add,285.events.threshold_delete = threshold_delete,286.events.threshold_flush = threshold_flush,287.events.threshold_up = threshold_up,288.events.threshold_down = threshold_down,289};290291static int thermal_event(__maybe_unused int fd, __maybe_unused void *arg)292{293struct thermal_data *td = arg;294295return thermal_events_handle(td->th, td);296}297298static void usage(const char *cmd)299{300printf("%s : A thermal monitoring engine based on notifications\n", cmd);301printf("Usage: %s [options]\n", cmd);302printf("\t-h, --help\t\tthis help\n");303printf("\t-d, --daemonize\n");304printf("\t-l <level>, --loglevel <level>\tlog level: ");305printf("DEBUG, INFO, NOTICE, WARN, ERROR\n");306printf("\t-s, --syslog\t\toutput to syslog\n");307printf("\n");308exit(0);309}310311static int options_init(int argc, char *argv[], struct options *options)312{313int opt;314315struct option long_options[] = {316{ "help", no_argument, NULL, 'h' },317{ "daemonize", no_argument, NULL, 'd' },318{ "syslog", no_argument, NULL, 's' },319{ "loglevel", required_argument, NULL, 'l' },320{ 0, 0, 0, 0 }321};322323while (1) {324325int optindex = 0;326327opt = getopt_long(argc, argv, "l:dhs", long_options, &optindex);328if (opt == -1)329break;330331switch (opt) {332case 'l':333options->loglevel = log_str2level(optarg);334break;335case 'd':336options->daemonize = 1;337break;338case 's':339options->logopt = TO_SYSLOG;340break;341case 'h':342usage(basename(argv[0]));343break;344default: /* '?' */345return -1;346}347}348349return 0;350}351352enum {353THERMAL_ENGINE_SUCCESS = 0,354THERMAL_ENGINE_OPTION_ERROR,355THERMAL_ENGINE_DAEMON_ERROR,356THERMAL_ENGINE_LOG_ERROR,357THERMAL_ENGINE_THERMAL_ERROR,358THERMAL_ENGINE_THRESHOLD_ERROR,359THERMAL_ENGINE_MAINLOOP_ERROR,360};361362int main(int argc, char *argv[])363{364struct thermal_data td;365struct options options = {366.loglevel = LOG_INFO,367.logopt = TO_STDOUT,368};369370if (options_init(argc, argv, &options)) {371ERROR("Usage: %s --help\n", argv[0]);372return THERMAL_ENGINE_OPTION_ERROR;373}374375if (options.daemonize && daemon(0, 0)) {376ERROR("Failed to daemonize: %p\n");377return THERMAL_ENGINE_DAEMON_ERROR;378}379380if (log_init(options.loglevel, basename(argv[0]), options.logopt)) {381ERROR("Failed to initialize logging facility\n");382return THERMAL_ENGINE_LOG_ERROR;383}384385td.th = thermal_init(&ops);386if (!td.th) {387ERROR("Failed to initialize the thermal library\n");388return THERMAL_ENGINE_THERMAL_ERROR;389}390391td.tz = thermal_zone_discover(td.th);392if (!td.tz) {393ERROR("No thermal zone available\n");394return THERMAL_ENGINE_THERMAL_ERROR;395}396397for_each_thermal_zone(td.tz, set_threshold, td.th);398399for_each_thermal_zone(td.tz, show_tz, td.th);400401if (mainloop_init()) {402ERROR("Failed to initialize the mainloop\n");403return THERMAL_ENGINE_MAINLOOP_ERROR;404}405406if (mainloop_add(thermal_events_fd(td.th), thermal_event, &td)) {407ERROR("Failed to setup the mainloop\n");408return THERMAL_ENGINE_MAINLOOP_ERROR;409}410411INFO("Waiting for thermal events ...\n");412413if (mainloop(-1)) {414ERROR("Mainloop failed\n");415return THERMAL_ENGINE_MAINLOOP_ERROR;416}417418return THERMAL_ENGINE_SUCCESS;419}420421422