xref: /haiku/headers/private/kernel/load_tracking.h (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
1 /*
2  * Copyright 2013 Paweł Dziepak, pdziepak@quarnos.org.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _KERNEL_LOAD_TRACKING_H
6 #define _KERNEL_LOAD_TRACKING_H
7 
8 
9 #include <OS.h>
10 
11 
12 const int32 kMaxLoad = 1000;
13 const bigtime_t kLoadMeasureInterval = 1000;
14 const bigtime_t kIntervalInaccuracy = kLoadMeasureInterval / 4;
15 
16 
17 static inline int32
18 compute_load(bigtime_t& measureTime, bigtime_t& measureActiveTime, int32& load,
19 	bigtime_t now)
20 {
21 	if (measureTime == 0) {
22 		measureTime = now;
23 		return -1;
24 	}
25 
26 	bigtime_t deltaTime = now - measureTime;
27 
28 	if (deltaTime < kLoadMeasureInterval)
29 		return -1;
30 
31 	int32 oldLoad = load;
32 	ASSERT(oldLoad >= 0 && oldLoad <= kMaxLoad);
33 
34 	int32 newLoad = measureActiveTime * kMaxLoad;
35 	newLoad /= max_c(deltaTime, 1);
36 	newLoad = max_c(min_c(newLoad, kMaxLoad), 0);
37 
38 	measureActiveTime = 0;
39 	measureTime = now;
40 
41 	deltaTime += kIntervalInaccuracy;
42 	bigtime_t n = deltaTime / kLoadMeasureInterval;
43 	ASSERT(n > 0);
44 
45 	if (n > 10)
46 		load = newLoad;
47 	else {
48 		newLoad *= (1 << n) - 1;
49 		load = (load + newLoad) / (1 << n);
50 		ASSERT(load >= 0 && load <= kMaxLoad);
51 	}
52 
53 	return oldLoad;
54 }
55 
56 
57 #endif	// _KERNEL_LOAD_TRACKING_H
58