1 /*
2  * Copyright (C) 2013-2021 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * This code is a complete clean re-write of the stress tool by
19  * Colin Ian King <colin.king@canonical.com> and attempts to be
20  * backwardly compatible with the stress tool by Amos Waterland
21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
22  * functionality.
23  *
24  */
25 #include "stress-ng.h"
26 
27 #define SECONDS_IN_MINUTE	(60.0)
28 #define SECONDS_IN_HOUR		(60.0 * SECONDS_IN_MINUTE)
29 #define SECONDS_IN_DAY		(24.0 * SECONDS_IN_HOUR)
30 #define SECONDS_IN_YEAR		(365.2425 * SECONDS_IN_DAY)
31 				/* Approx, for Gregorian calendar */
32 
33 /*
34  *  stress_timeval_to_double()
35  *      convert timeval to seconds as a double
36  */
stress_timeval_to_double(const struct timeval * tv)37 double stress_timeval_to_double(const struct timeval *tv)
38 {
39 	return (double)tv->tv_sec + ((double)tv->tv_usec * ONE_MILLIONTH);
40 }
41 
42 /*
43  *  stress_time_now()
44  *	time in seconds as a double
45  */
stress_time_now(void)46 double stress_time_now(void)
47 {
48 	struct timeval now;
49 
50 	if (gettimeofday(&now, NULL) < 0)
51 		return -1.0;
52 
53 	return stress_timeval_to_double(&now);
54 }
55 
56 /*
57  *  stress_format_time()
58  *	format a unit of time into human readable format
59  */
stress_format_time(const bool last,const double secs_in_units,const char * units,char ** ptr,double * duration,size_t * len)60 static inline void stress_format_time(
61 	const bool last,		/* Last unit to format */
62 	const double secs_in_units,	/* Seconds in the specific time unit */
63 	const char *units,		/* Unit of time */
64 	char **ptr,			/* Destination string ptr */
65 	double *duration,		/* Duration left in seconds */
66 	size_t *len)			/* Length of string left at ptr */
67 {
68 	const unsigned long val = (unsigned long)(*duration / secs_in_units);
69 
70 	if (last || (val > 0)) {
71 		int ret;
72 
73 		if (last)
74 			ret = snprintf(*ptr, *len, "%.2f %ss", *duration, units);
75 		else
76 			ret = snprintf(*ptr, *len, "%lu %s%s, ", val, units,
77 				(val > 1) ? "s" : "");
78 		if (ret > 0) {
79 			*len -= (size_t)ret;
80 			*ptr += ret;
81 		}
82 	}
83 	*duration -= secs_in_units * (double)val;
84 }
85 
86 /*
87  *  stress_duration_to_str
88  *	duration in seconds to a human readable string
89  */
stress_duration_to_str(const double duration)90 const char *stress_duration_to_str(const double duration)
91 {
92 	static char str[128];
93 	char *ptr = str;
94 	size_t len = sizeof(str) - 1;
95 	double dur = duration;
96 
97 	*str = '\0';
98 	if (duration > 60.0) {
99 		(void)shim_strlcpy(ptr, " (", len);
100 		ptr += 2;
101 		len -= 2;
102 		stress_format_time(false, SECONDS_IN_YEAR, "year", &ptr, &dur, &len);
103 		stress_format_time(false, SECONDS_IN_DAY, "day", &ptr, &dur, &len);
104 		stress_format_time(false, SECONDS_IN_HOUR, "hour", &ptr, &dur, &len);
105 		stress_format_time(false, SECONDS_IN_MINUTE, "min", &ptr, &dur, &len);
106 		stress_format_time(true, 1, "sec", &ptr, &dur, &len);
107 		(void)shim_strlcpy(ptr, ")", len);
108 	}
109 	return str;
110 }
111