source: mainline/kernel/generic/src/ps/load.c@ 6ec34bb

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

System load echo fix, now it looks realistic
top echoes first values (uptime)
Several ps and uptime fixes

  • 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 <ps/load.h>
40#include <arch.h>
41#include <proc/scheduler.h>
42#include <config.h>
43#include <arch/types.h>
44#include <time/clock.h>
45#include <syscall/copy.h>
46
47static size_t get_running_count(void);
48
49unsigned long avenrun[3];
50
51#define FSHIFT 11 /* nr of bits of precision */
52#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
53#define LOAD_FREQ 5 /* 5 sec intervals */
54#define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point */
55#define EXP_5 2014 /* 1/exp(5sec/5min) */
56#define EXP_15 2037 /* 1/exp(5sec/15min) */
57
58void get_avenrun(unsigned long *loads, int shift)
59{
60 loads[0] = avenrun[0] << shift;
61 loads[1] = avenrun[1] << shift;
62 loads[2] = avenrun[2] << shift;
63}
64
65static inline unsigned long calc_load(unsigned long load, size_t exp, size_t active)
66{
67 load *= exp;
68 load += active * (FIXED_1 - exp);
69 return load >> FSHIFT;
70}
71
72static inline void calc_load_global(void)
73{
74 size_t active;
75
76 active = get_running_count();
77 active = active > 0 ? active * FIXED_1 : 0;
78 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
79 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
80 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
81}
82
83static size_t get_running_count(void)
84{
85 size_t i;
86 size_t result = 0;
87 ipl_t ipl;
88
89 /* run queues should not change during reading */
90 ipl = interrupts_disable();
91
92 for (i = 0; i < config.cpu_active; ++i) {
93 cpu_t *cpu = &cpus[i];
94 int j;
95 for (j = 0; j < RQ_COUNT; ++j) {
96 result += cpu->rq[j].n;
97 }
98 }
99
100 interrupts_restore(ipl);
101 return result;
102}
103
104/** Load thread main function.
105 * Thread computes system load every few seconds.
106 *
107 * @param arg Generic thread argument (unused).
108 *
109 */
110void kload_thread(void *arg)
111{
112 /* Noone will thread_join us */
113 thread_detach(THREAD);
114 avenrun[0] = 0;
115 avenrun[1] = 0;
116 avenrun[2] = 0;
117
118 while (true) {
119 calc_load_global();
120 thread_sleep(LOAD_FREQ);
121 }
122}
123
124int sys_ps_get_load(unsigned long *user_load)
125{
126 unsigned long loads[3];
127 get_avenrun(loads, 5);
128 copy_to_uspace(user_load, loads, sizeof(loads));
129 return 0;
130}
131
132
133/** @}
134 */
Note: See TracBrowser for help on using the repository browser.