summaryrefslogtreecommitdiff
path: root/src/process.c
blob: 93acf46f6faa43874c6cbf449a9a677ae6dc3064 (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
163
164
165
166
167
168
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Flora License, Version 1.1 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "process.h"
#include "clock.h"
#include "sys-stats.h"
#include "err-check.h"
#include "procfs.h"
#include "appinfo-provider.h"
#include "json-schema-defs.h"

static struct sys_stats sys;
static int pagesize;
static float timescale;

int process_get_memory_usage(struct process *proc, int *usage)
{
	ON_NULL_RETURN_VAL(proc, -1);
	ON_NULL_RETURN_VAL(usage, -1);

	*usage = proc->memory_used;
	return 0;
}

int process_get_cpu_usage(struct process *proc, unsigned long long *usage)
{
	ON_NULL_RETURN_VAL(proc, -1);
	ON_NULL_RETURN_VAL(usage, -1);

	*usage = proc->frame_ticks_used;
	return 0;
}

int process_update(struct process *proc)
{
	struct procfs_process_stat info;
	unsigned long long ticks;

	if (procfs_read_process_stat(proc->pid, &info) != 0) {
		return -1;
	}
	ticks = info.stime + info.utime;
	proc->frame_ticks_used = ticks - proc->total_ticks_used;
	proc->total_ticks_used = ticks;
	proc->memory_used = info.rss * pagesize / 1024;
	float now = clock_monotonic_get();
	proc->frame_time_inverted = 1.0f / (now - proc->update_time);
	proc->update_time = now;

	return 0;
}

void process_init_process(int pid, struct process *proc)
{
	memset(proc, 0x0, sizeof(struct process));
	proc->pid = pid;
}

int process_get_pid(const struct process *proc)
{
	ON_NULL_RETURN_VAL(proc, -1);
	return proc->pid;
}

const char *process_get_appid(struct process *proc)
{
	ON_NULL_RETURN_VAL(proc, NULL);

	if (!proc->appid) {
		proc->appid = app_info_provider_find_app_id(proc->pid);
	}
	return proc->appid;
}

const char *process_get_exe(struct process *proc)
{
	ON_NULL_RETURN_VAL(proc, NULL);

	if (!proc->exe) {
		if (procfs_read_process_exe(proc->pid, &proc->exe) != 0)
			return NULL;
	}
	return proc->exe;
}

void process_shutdown(struct process *proc)
{
	if (!proc) return;
	free(proc->appid);
	free(proc->exe);
	memset(proc, 0x0, sizeof(struct process));
}

void process_move(struct process *dst, struct process *src)
{
	memcpy(dst, src, sizeof(struct process));
	src->appid = NULL;
	src->exe = NULL;
}

int process_get_cpu_usage_percentage(struct process *proc, float *usage)
{
	ON_NULL_RETURN_VAL(proc, -1);
	ON_NULL_RETURN_VAL(usage, -1);

	*usage = (float)proc->frame_ticks_used * proc->frame_time_inverted * timescale;
	return 0;
}

int process_get_memory_usage_percentage(struct process *proc, float *usage)
{
	ON_NULL_RETURN_VAL(proc, -1);
	ON_NULL_RETURN_VAL(usage, -1);

	*usage = (float)proc->memory_used / sys.total_memory;
	return 0;
}

int process_init()
{
	if (sys_stats_update(&sys) != 0) {
		ERR("stats_update_system_stats failed.");
		return -1;
	}
	timescale = 1.0f / (float)sysconf(_SC_CLK_TCK);
	pagesize = getpagesize();
	return 0;
}

void process_serialize(struct process *proc, JsonBuilder *builder)
{
	ON_NULL_RETURN(proc);

	float tmp;
	json_builder_begin_object(builder);

	json_builder_set_member_name(builder, SCHEMA_RESULT_APP_ID);
	json_builder_add_string_value(builder, process_get_appid(proc));
	json_builder_set_member_name(builder, SCHEMA_RESULT_EXE);
	json_builder_add_string_value(builder, process_get_exe(proc));
	json_builder_set_member_name(builder, SCHEMA_RESULT_PID);
	json_builder_add_int_value(builder, process_get_pid(proc));
	json_builder_set_member_name(builder, SCHEMA_RESULT_CPU);
	process_get_cpu_usage_percentage(proc, &tmp);
	json_builder_add_double_value(builder, tmp);
	json_builder_set_member_name(builder, SCHEMA_RESULT_MEMORY);
	process_get_memory_usage_percentage(proc, &tmp);
	json_builder_add_double_value(builder, tmp);

	json_builder_end_object(builder);
}