source: mainline/kernel/generic/src/ps/load.c@ 944f2cab

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 944f2cab was 944f2cab, checked in by Stanislav Kozina <stanislav.kozina@…>, 16 years ago

ps -l echoes system load

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * Copyright (c) 2010 Stanislav Kozina
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genericload
30 * @{
31 */
32
33/**
34 * @file
35 * @brief System load computation.
36 */
37
38#include <proc/thread.h>
39#include <print.h>
40#include <ps/load.h>
41#include <arch.h>
42#include <proc/scheduler.h>
43#include <config.h>
44#include <arch/types.h>
45#include <time/clock.h>
46#include <syscall/copy.h>
47
48static size_t get_running_count(void);
49
50size_t avenrun[3];
51
52#define FSHIFT 11 /* nr of bits of precision */
53#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
54#define LOAD_FREQ 5 /* 5 sec intervals */
55#define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point */
56#define EXP_5 2014 /* 1/exp(5sec/5min) */
57#define EXP_15 2037 /* 1/exp(5sec/15min) */
58
59#define CALC_LOAD(load,exp,n) \
60 load *= exp; \
61 load += n*(FIXED_1-exp); \
62 load >>= FSHIFT;
63
64static inline unsigned long calc_load(size_t load, size_t exp, size_t active)
65{
66 load *= exp;
67 load += active * (FIXED_1 - exp);
68 return load >> FSHIFT;
69}
70
71static inline void calc_load_global(void)
72{
73 size_t active;
74
75 active = get_running_count();
76 active = active > 0 ? active * FIXED_1 : 0;
77 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
78 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
79 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
80}
81
82static size_t get_running_count(void)
83{
84 size_t i;
85 size_t result = 0;
86 ipl_t ipl;
87
88 /* run queues should not change during reading */
89 ipl = interrupts_disable();
90
91 for (i = 0; i < config.cpu_active; ++i) {
92 cpu_t *cpu = &cpus[i];
93 int j;
94 for (j = 0; j < RQ_COUNT; ++j) {
95 result += cpu->rq[j].n;
96 }
97 }
98
99 interrupts_restore(ipl);
100 return result;
101}
102
103/** Load thread main function.
104 * Thread computes system load every few seconds.
105 *
106 * @param arg Generic thread argument (unused).
107 *
108 */
109void kload_thread(void *arg)
110{
111 /* Noone will thread_join us */
112 thread_detach(THREAD);
113 avenrun[0] = 0;
114 avenrun[1] = 0;
115 avenrun[2] = 0;
116
117 while (true) {
118 calc_load_global();
119 printf("Computed loads: 0x%x 0x%x 0x%x\n", avenrun[0], avenrun[1], avenrun[2]);
120 thread_sleep(LOAD_FREQ);
121 }
122}
123
124int sys_ps_get_load(size_t *user_load)
125{
126 copy_to_uspace(user_load, avenrun, sizeof(avenrun));
127 return 0;
128}
129
130
131/** @}
132 */
Note: See TracBrowser for help on using the repository browser.