summaryrefslogtreecommitdiff
path: root/usr/src/cmd/latencytop/common/util.c
blob: 0d4173f196f74dff431e6f9c18d416138be31e71 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2008-2009, Intel Corporation.
 * All Rights Reserved.
 */

#include <unistd.h>
#include <libintl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <procfs.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "latencytop.h"

/* Pipe that breaks the event loop (and exits early) */
static int signal_pipe[2];

/*
 * Get current system time in milliseconds (1e-3).
 */
uint64_t
lt_millisecond(void)
{
	struct timeval p;
	(void) gettimeofday(&p, NULL);
	return ((uint64_t)p.tv_sec * 1000 + p.tv_usec / 1000);
}

/*
 * Check if we are out of memory.
 */
void
lt_check_null(void *p)
{
	if (p == NULL) {
		(void) fprintf(stderr, "Out of memory!\n");
		g_assert(0);
		exit(2);
	}
}

/*
 * Safe malloc.
 */
void *
lt_malloc(size_t size)
{
	void *ret = malloc(size);

	lt_check_null(ret);

	return (ret);
}

/*
 * Safe alloc with memory cleared.
 * It is named "zalloc" because its signature is different from
 * calloc() in stdlib.
 */
void *
lt_zalloc(size_t size)
{
	void *ret = calloc(size, 1);

	lt_check_null(ret);

	return (ret);
}

/*
 * Safe strdup.
 */
char *
lt_strdup(const char *str)
{
	char *ret = strdup(str);

	lt_check_null(ret);

	return (ret);
}

/*
 * Get string for current time, e.g. YYYY-MM-DD
 */
void
lt_time_str(char *buffer, int len)
{
	struct tm tms;
	time_t t;
	int i;

	(void) time(&t);
	(void) gmtime_r(&t, &tms);
	(void) asctime_r(&tms, buffer, len);

	for (i = strlen(buffer)-1; i > 0; --i) {

		if (isspace(buffer[i])) {
			buffer[i] = '\0';
		} else {
			break;
		}
	}
}

/*
 * Retrieves the process's executable name and arguments from /proc.
 */
char *
lt_get_proc_field(pid_t pid, lt_field_t field)
{
	char name[PATH_MAX];
	int fd;
	int ret;
	psinfo_t psinfo;

	(void) snprintf(name, PATH_MAX, "/proc/%d/psinfo", (int)pid);
	fd = open(name, O_RDONLY);

	if (fd == -1) {
		return (NULL);
	}

	ret = read(fd, (char *)&psinfo, sizeof (psinfo_t));
	(void) close(fd);

	if (ret < 0) {
		return (NULL);
	}

	switch (field) {
	case LT_FIELD_FNAME:
		return (lt_strdup(psinfo.pr_fname));
	case LT_FIELD_PSARGS:
		return (lt_strdup(psinfo.pr_psargs));
	}
	return (NULL);
}

/*
 * Helper function to update the data structure.
 */
void
lt_update_stat_value(lt_stat_data_t *entry,
    lt_stat_type_t type, uint64_t value)
{
	switch (type) {
	case LT_STAT_COUNT:
		entry->lt_s_count += value;
		break;
	case LT_STAT_SUM:
		entry->lt_s_total += value;
		break;
	case LT_STAT_MAX:
		if (value > entry->lt_s_max) {
			entry->lt_s_max = value;
		}
		break;
	default:
		break;
	}
}

/*
 * Helper function to sort on total.
 */
int
lt_sort_by_total_desc(lt_stat_entry_t *a, lt_stat_entry_t *b)
{
	g_assert(a != NULL && b != NULL);
	/*
	 * lt_s_total is of type int64_t, so we can't simply return
	 * (b->lt_se_data.lt_s_total - a->lt_se_data.lt_s_total).
	 */
	if (b->lt_se_data.lt_s_total > a->lt_se_data.lt_s_total) {
		return (1);
	} else if (b->lt_se_data.lt_s_total < a->lt_se_data.lt_s_total) {
		return (-1);
	} else {
		return (0);
	}
}

/*
 * Helper function to sort on max.
 */
int
lt_sort_by_max_desc(lt_stat_entry_t *a, lt_stat_entry_t *b)
{
	g_assert(a != NULL && b != NULL);

	if (b->lt_se_data.lt_s_max > a->lt_se_data.lt_s_max) {
		return (1);
	} else if (b->lt_se_data.lt_s_max < a->lt_se_data.lt_s_max) {
		return (-1);
	} else {
		return (0);
	}
}

/*
 * Helper function to sort on count.
 */
int
lt_sort_by_count_desc(lt_stat_entry_t *a, lt_stat_entry_t *b)
{
	g_assert(a != NULL && b != NULL);

	if (b->lt_se_data.lt_s_count > a->lt_se_data.lt_s_count) {
		return (1);
	} else if (b->lt_se_data.lt_s_count < a->lt_se_data.lt_s_count) {
		return (-1);
	} else {
		return (0);
	}
}

/*
 * Helper function to sort on average.
 */
int
lt_sort_by_avg_desc(lt_stat_entry_t *a, lt_stat_entry_t *b)
{
	double avg_a, avg_b;

	g_assert(a != NULL && b != NULL);

	avg_a = (double)a->lt_se_data.lt_s_total / a->lt_se_data.lt_s_count;
	avg_b = (double)b->lt_se_data.lt_s_total / b->lt_se_data.lt_s_count;

	if (avg_b > avg_a) {
		return (1);
	} else if (avg_b < avg_a) {
		return (-1);
	} else {
		return (0);
	}
}

/*
 * Create pipe for signal handler and wakeup.
 */
void
lt_gpipe_init(void)
{
	(void) pipe(signal_pipe);
}

/*
 * Close the pipe used in signal handler.
 */
void
lt_gpipe_deinit(void)
{
	(void) close(signal_pipe[0]);
	(void) close(signal_pipe[1]);
}

/*
 * Break early from the main loop.
 */
void
lt_gpipe_break(const char *ch)
{
	(void) write(signal_pipe[1], ch, 1);
}

int
lt_gpipe_readfd(void)
{
	return (signal_pipe[0]);
}

/*
 * Check if the given file exists.
 */
int
lt_file_exist(const char *name)
{
	struct stat64 st;

	if (stat64(name, &st) == 0) {
		return (1);
	} else {
		return (0);
	}
}