aboutsummaryrefslogtreecommitdiffstats
path: root/argparse.c
blob: e748b3e57324775395c05a31122d0d2894f5d19d (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "cputemp2maxfreq.h"
#include "debug.h"
#include "logger.h"

extern struct s_config config;

void printhelp()
{
 printf(
  "Usage: %s <options>\n"
  "\n"
  "Scaling options:\n"
  "-d <delay>     Transition latency in us or \"auto\" for autodetect\n"
  "               Default: auto\n"
  "-f <frequency> Fallback frequency in KHz, used when everything fails\n"
  "               Default: %ld\n"
  "-g <governor>  cpufreq governor to use, use \"keep\" to keep current governor\n"
  "               Default: %s\n"
  "-i <sensor>    Input for temperature readout, this can be specified as:\n"
  "               A full path that starts with /, usually somewhere in sysfs\n"
  "               A sensor name, for example \"Core 0\"\n"
  "               The word \"auto\" autodetects a valid sensor\n"
  "               Default: %s\n"
  "-k             Keep last state on successful exit, set CPU to lowest frequency\n"
  "               on error.\n"
  "               Default: Always set CPU to lowest frequency on exit\n"
  "-p <time>      Poll interval in seconds\n"
  "               Default: %d\n"
  "-P <CPU ID>    Physical CPU number (socket number) for multi-CPU systems. Given\n"
  "               a number it will only change the governor/frequency for the\n"
  "               given CPU, or the word \"all\" to change all CPU's\n"
  "               Default: all\n"
  "-s <step>      Step size in Khz when increasing/decreasing CPU speed\n"
  "               Default: %ld\n"
  "-t <number>    Temperature limit\n"
  "               Default: %ld\n"
  "\n"
  "Logging options:\n"
  "-c <file>      Write CSV log for later analysis to file. Append data if the\n"
  "               file already exists\n"
  "-C <file>      Same as -c but overwrite existing file\n"
  "-l <logger>    Logger to use, valid values: none, kmsg, stdout, syslog\n"
  "               Default: %s\n"
  "-m             Log measurements (CPU temperature and frequency) via logger\n"
  "-u             Use unixtime timestamps in log and CSV outputs\n"
  "               Default timestamp format is: YYYY-MM-DD HH:MM:SS\n"
  "\n"
  "Other options:\n"
  "-h             Display this help text\n",
  config.name,config.fallback_freq,config.governor,config.temp_input,config.interval,
  config.freq_step,config.max_temp/1000,config.logger_name
 );
}

void argparse(int argc, char **argv)
{
 int opt;
 struct s_config userconfig;
 char *lastslash;
 DEBUG1_ARGPARSE("Started\n");

 lastslash=strrchr(argv[0],'/');
 if (lastslash==NULL)
 {
  lastslash=argv[0];
 } else {
  lastslash++;
 }
 strncpy(config.name,lastslash,sizeof(config.name));
 DEBUG2_ARGPARSE("Detected program name %s\n",config.name);

// Copy defaults
 memcpy(&userconfig,&config,sizeof(struct s_config));

// Parse options
 while((opt=getopt(argc,argv,"c:C:d:f:g:hi:kl:mp:P:s:t:u"))!=-1)
 {
  DEBUG1_ARGPARSE("Argument: %c Value: %s\n",opt,optarg);
  switch(opt)
  {
   case 'c':
    strncpy(userconfig.csvlog,optarg,sizeof(userconfig.csvlog));
    userconfig.csvoverwrite=0;
    break;
   case 'C':
    strncpy(userconfig.csvlog,optarg,sizeof(userconfig.csvlog));
    userconfig.csvoverwrite=1;
    break;
   case 'd':
    if (strcmp("auto",optarg)==0)
    {
     userconfig.transition_latency=-1;
    } else {
     userconfig.transition_latency=strtoll(optarg,NULL,10);
    }
    break;
   case 'f':
    userconfig.fallback_freq=strtoll(optarg,NULL,10);
    break;
   case 'g':
    strncpy(userconfig.governor,optarg,sizeof(userconfig.governor));
    break;
   case 'h':
    printhelp();
    exit(1);
   case 'i':
    strncpy(userconfig.temp_input,optarg,sizeof(userconfig.temp_input));
    break;
   case 'k':
    userconfig.keepstate=1;
    break;
   case 'l':
    strncpy(userconfig.logger_name,optarg,sizeof(userconfig.logger_name));
    break;
   case 'm':
    userconfig.log_data=1;
    break;
   case 'p':
    userconfig.interval=(unsigned int) strtoll(optarg,NULL,10);
    break;
   case 'P':
    if (strcmp(optarg,"all")==0)
    {
     userconfig.cpu=-1;
    } else {
     userconfig.cpu=(int) strtoll(optarg,NULL,10);
    }
    break;
   case 's':
    userconfig.freq_step=strtoll(optarg,NULL,10);
    break;
   case 't':
    userconfig.max_temp=strtoll(optarg,NULL,10)*1000;
    break;
   case 'u':
    userconfig.use_unixtime=1;
    break;
   default:
    printhelp();
    exit(1);
  }
 }

// Change the logger function pointer
 DEBUG1_ARGPARSE("Logger is set to %s\n",userconfig.logger_name);
 if (strcmp(userconfig.logger_name,"none")==0) userconfig.logger=&logger_none;
 else if (strcmp(userconfig.logger_name,"stdout")==0) userconfig.logger=&logger_stdout;
 else if (strcmp(userconfig.logger_name,"syslog")==0) userconfig.logger=&logger_syslog;
 else if (strcmp(userconfig.logger_name,"kmsg")==0) userconfig.logger=&logger_kmsg;
 else {
  config.logger("Invalid value for logger, exiting");
  exit(1);
 }
 DEBUG2_ARGPARSE("Logger pointer is %p\n",userconfig.logger);

// Copy modified config to global config
 memcpy(&config,&userconfig,sizeof(struct s_config));
}