bootchart: kill a bunch of global variables

This code is in a confusingly bad shape. Kill a bunch of global variables
and pass needed variables around in function calls.
This commit is contained in:
Daniel Mack
2015-04-03 15:29:18 +02:00
parent f91781329c
commit 1f2ecb0393
6 changed files with 393 additions and 377 deletions
+37 -18
View File
@@ -58,14 +58,6 @@
#include "bootchart.h"
#include "list.h"
double graph_start;
double log_start;
struct ps_struct *ps_first;
int pscount;
int cpus;
double interval;
FILE *of = NULL;
int overrun = 0;
static int exiting = 0;
#define DEFAULT_SAMPLES_LEN 500
@@ -77,20 +69,17 @@ static int exiting = 0;
/* graph defaults */
bool arg_entropy = false;
bool initcall = true;
bool arg_initcall = true;
bool arg_relative = false;
bool arg_filter = true;
bool arg_show_cmdline = false;
bool arg_show_cgroup = false;
bool arg_pss = false;
bool arg_percpu = false;
int samples;
int arg_samples_len = DEFAULT_SAMPLES_LEN; /* we record len+1 (1 start sample) */
double arg_hz = DEFAULT_HZ;
double arg_scale_x = DEFAULT_SCALE_X;
double arg_scale_y = DEFAULT_SCALE_Y;
static struct list_sample_data *sampledata;
struct list_sample_data *head;
char arg_init_path[PATH_MAX] = DEFAULT_INIT;
char arg_output_path[PATH_MAX] = DEFAULT_OUTPUT;
@@ -315,6 +304,11 @@ int main(int argc, char *argv[]) {
_cleanup_free_ char *build = NULL;
_cleanup_close_ int sysfd = -1;
_cleanup_closedir_ DIR *proc = NULL;
_cleanup_fclose_ FILE *of = NULL;
double graph_start;
double log_start;
double interval;
struct ps_struct *ps_first;
struct sigaction sig = {
.sa_handler = signal_handler,
};
@@ -323,7 +317,13 @@ int main(int argc, char *argv[]) {
char datestr[200];
time_t t = 0;
int r;
int pscount = 0;
int n_cpus = 0;
int overrun = 0;
int samples;
struct rlimit rlim;
struct list_sample_data *head;
static struct list_sample_data *sampledata;
parse_conf();
@@ -364,7 +364,18 @@ int main(int argc, char *argv[]) {
interval = (1.0 / arg_hz) * 1000000000.0;
log_uptime();
if (arg_relative)
graph_start = log_start = gettime_ns();
else {
struct timespec n;
double uptime;
clock_gettime(CLOCK_BOOTTIME, &n);
uptime = (n.tv_sec + (n.tv_nsec / (double) NSEC_PER_SEC));
log_start = gettime_ns();
graph_start = log_start - uptime;
}
if (graph_start < 0.0) {
log_error("Failed to setup graph start time.\n\n"
@@ -409,7 +420,7 @@ int main(int argc, char *argv[]) {
/* wait for /proc to become available, discarding samples */
if (proc) {
r = log_sample(proc, samples, &sampledata);
r = log_sample(proc, samples, ps_first, &sampledata, &pscount, &n_cpus);
if (r < 0)
return EXIT_FAILURE;
}
@@ -475,7 +486,18 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
r = svg_do(strna(build));
r = svg_do(of,
strna(build),
head,
ps_first,
samples,
pscount,
n_cpus,
graph_start,
log_start,
interval,
overrun);
if (r < 0) {
log_error_errno(r, "Error generating svg file: %m\n");
return EXIT_FAILURE;
@@ -485,9 +507,6 @@ int main(int argc, char *argv[]) {
do_journal_append(output_file);
if (of)
fclose(of);
/* nitpic cleanups */
ps = ps_first->next_ps;
while (ps->next_ps) {
+2 -16
View File
@@ -103,14 +103,6 @@ struct ps_struct {
struct ps_sched_struct *sample;
};
extern int entropy_avail[];
extern double graph_start;
extern double log_start;
extern double sampletime[];
extern struct ps_struct *ps_first;
extern struct block_stat_struct blockstat[];
extern int pscount;
extern bool arg_relative;
extern bool arg_filter;
extern bool arg_show_cmdline;
@@ -118,17 +110,11 @@ extern bool arg_show_cgroup;
extern bool arg_pss;
extern bool arg_entropy;
extern bool arg_percpu;
extern bool initcall;
extern int samples;
extern int cpus;
extern int arg_samples_len;
extern bool arg_initcall;
extern int arg_samples_len;
extern double arg_hz;
extern double arg_scale_x;
extern double arg_scale_y;
extern int overrun;
extern double interval;
extern char arg_output_path[PATH_MAX];
extern char arg_init_path[PATH_MAX];
extern FILE *of;
+11 -23
View File
@@ -54,24 +54,6 @@ double gettime_ns(void) {
return (n.tv_sec + (n.tv_nsec / (double) NSEC_PER_SEC));
}
static double gettime_up(void) {
struct timespec n;
clock_gettime(CLOCK_BOOTTIME, &n);
return (n.tv_sec + (n.tv_nsec / (double) NSEC_PER_SEC));
}
void log_uptime(void) {
if (arg_relative)
graph_start = log_start = gettime_ns();
else {
double uptime = gettime_up();
log_start = gettime_ns();
graph_start = log_start - uptime;
}
}
static char *bufgetline(char *buf) {
char *c;
@@ -105,7 +87,13 @@ static int pid_cmdline_strscpy(int procfd, char *buffer, size_t buf_len, int pid
return 0;
}
int log_sample(DIR *proc, int sample, struct list_sample_data **ptr) {
int log_sample(DIR *proc,
int sample,
struct ps_struct *ps_first,
struct list_sample_data **ptr,
int *pscount,
int *cpus) {
static int vmstat = -1;
static int schedstat = -1;
char buf[4096];
@@ -195,8 +183,8 @@ vmstat_next:
sampledata->runtime[c] = atoll(rt);
sampledata->waittime[c] = atoll(wt);
if (c == cpus)
cpus = c + 1;
if (c == *cpus)
*cpus = c + 1;
}
schedstat_next:
m = bufgetline(m);
@@ -263,7 +251,7 @@ schedstat_next:
ps->sample->sampledata = sampledata;
pscount++;
(*pscount)++;
/* mark our first sample */
ps->first = ps->last = ps->sample;
@@ -480,7 +468,7 @@ schedstat_next:
catch_rename:
/* catch process rename, try to randomize time */
mod = (arg_hz < 4.0) ? 4.0 : (arg_hz / 4.0);
if (((samples - ps->pid) + pid) % (int)(mod) == 0) {
if (((sample - ps->pid) + pid) % (int)(mod) == 0) {
/* re-fetch name */
/* get name, start time */
+6 -1
View File
@@ -29,4 +29,9 @@
double gettime_ns(void);
void log_uptime(void);
int log_sample(DIR *proc, int sample, struct list_sample_data **ptr);
int log_sample(DIR *proc,
int sample,
struct ps_struct *ps_first,
struct list_sample_data **ptr,
int *pscount,
int *cpus);
+326 -318
View File
File diff suppressed because it is too large Load Diff
+11 -1
View File
@@ -24,4 +24,14 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
int svg_do(const char *build);
int svg_do(FILE *of,
const char *build,
struct list_sample_data *head,
struct ps_struct *ps_first,
int n_samples,
int pscount,
int n_cpus,
double graph_start,
double log_start,
double interval,
int overrun);