source: mainline/kernel/generic/src/time/clock.c@ 70e2b2d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 70e2b2d was 9dae191e, checked in by Martin Decky <martin@…>, 16 years ago

sysinfo API cleanup

  • better support for generated subtrees
  • synchronization
  • memory management (generated items cleanup)
  • simplier sysinfo_dump()

remove separate statistical syscalls, replace with virtual sysinfo items (some functionality is still missing)

  • naming consolidation
  • cleaner API
  • proper synchronization

minor renames

  • zone_print_list() → zones_print_list()
  • zone_busy_and_free() → zones_stats()
  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[f761f1eb]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
[1bb2e7a]29/** @addtogroup time
[b45c443]30 * @{
31 */
32
[cf26ba9]33/**
[b45c443]34 * @file
[cf26ba9]35 * @brief High-level clock interrupt handler.
36 *
37 * This file contains the clock() function which is the source
38 * of preemption. It is also responsible for executing expired
39 * timeouts.
40 */
41
[f761f1eb]42#include <time/clock.h>
43#include <time/timeout.h>
44#include <config.h>
45#include <synch/spinlock.h>
46#include <synch/waitq.h>
47#include <func.h>
48#include <proc/scheduler.h>
49#include <cpu.h>
50#include <arch.h>
[5c9a08b]51#include <adt/list.h>
[23684b7]52#include <atomic.h>
[1084a784]53#include <proc/thread.h>
[d6e5cbc]54#include <sysinfo/sysinfo.h>
55#include <arch/barrier.h>
[f8ddd17]56#include <mm/frame.h>
57#include <ddi/ddi.h>
58
[4b662f8c]59/* Pointer to variable with uptime */
60uptime_t *uptime;
61
62/** Physical memory area of the real time clock */
[f8ddd17]63static parea_t clock_parea;
[d6e5cbc]64
65/* Variable holding fragment of second, so that we would update
66 * seconds correctly
67 */
[7f1c620]68static unative_t secfrag = 0;
[d6e5cbc]69
70/** Initialize realtime clock counter
71 *
72 * The applications (and sometimes kernel) need to access accurate
73 * information about realtime data. We allocate 1 page with these
74 * data and update it periodically.
75 */
76void clock_counter_init(void)
77{
78 void *faddr;
79
[f8ddd17]80 faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC);
[d6e5cbc]81 if (!faddr)
[f651e80]82 panic("Cannot allocate page for clock.");
[d6e5cbc]83
[4b662f8c]84 uptime = (uptime_t *) PA2KA(faddr);
85
86 uptime->seconds1 = 0;
87 uptime->seconds2 = 0;
[9dae191e]88 uptime->useconds = 0;
[d6e5cbc]89
[f8ddd17]90 clock_parea.pbase = (uintptr_t) faddr;
91 clock_parea.frames = 1;
92 ddi_parea_register(&clock_parea);
93
94 /*
95 * Prepare information for the userspace so that it can successfully
96 * physmem_map() the clock_parea.
97 */
98 sysinfo_set_item_val("clock.cacheable", NULL, (unative_t) true);
99 sysinfo_set_item_val("clock.faddr", NULL, (unative_t) faddr);
[d6e5cbc]100}
101
102
103/** Update public counters
104 *
105 * Update it only on first processor
106 * TODO: Do we really need so many write barriers?
107 */
108static void clock_update_counters(void)
109{
110 if (CPU->id == 0) {
[4b662f8c]111 secfrag += 1000000 / HZ;
[d6e5cbc]112 if (secfrag >= 1000000) {
[fd8302d]113 secfrag -= 1000000;
[4b662f8c]114 uptime->seconds1++;
[d6e5cbc]115 write_barrier();
[4b662f8c]116 uptime->useconds = secfrag;
[fd8302d]117 write_barrier();
[4b662f8c]118 uptime->seconds2 = uptime->seconds1;
[d6e5cbc]119 } else
[4b662f8c]120 uptime->useconds += 1000000 / HZ;
[d6e5cbc]121 }
122}
[f761f1eb]123
[70527f1]124/** Clock routine
125 *
126 * Clock routine executed from clock interrupt handler
[22f7769]127 * (assuming interrupts_disable()'d). Runs expired timeouts
[70527f1]128 * and preemptive scheduling.
129 *
[f761f1eb]130 */
131void clock(void)
132{
133 link_t *l;
134 timeout_t *h;
[ba1b2194]135 timeout_handler_t f;
[f761f1eb]136 void *arg;
[98000fb]137 size_t missed_clock_ticks = CPU->missed_clock_ticks;
[6c441cf8]138 unsigned int i;
[f761f1eb]139
[e257ae3]140 /* Account lost ticks to CPU usage */
141 if (CPU->idle) {
142 ASSERT(missed_clock_ticks == 0);
143 CPU->idle_ticks++;
144 } else {
145 CPU->busy_ticks += missed_clock_ticks + 1;
146 }
147 CPU->idle = false;
148
[f761f1eb]149 /*
150 * To avoid lock ordering problems,
151 * run all expired timeouts as you visit them.
152 */
[8cf8ee6]153 for (i = 0; i <= missed_clock_ticks; i++) {
[d6e5cbc]154 clock_update_counters();
[c93e805]155 spinlock_lock(&CPU->timeoutlock);
156 while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) {
157 h = list_get_instance(l, timeout_t, link);
158 spinlock_lock(&h->lock);
159 if (h->ticks-- != 0) {
160 spinlock_unlock(&h->lock);
161 break;
162 }
163 list_remove(l);
164 f = h->handler;
165 arg = h->arg;
166 timeout_reinitialize(h);
167 spinlock_unlock(&h->lock);
168 spinlock_unlock(&CPU->timeoutlock);
[f761f1eb]169
[c93e805]170 f(arg);
[f761f1eb]171
[c93e805]172 spinlock_lock(&CPU->timeoutlock);
173 }
174 spinlock_unlock(&CPU->timeoutlock);
[f761f1eb]175 }
[c93e805]176 CPU->missed_clock_ticks = 0;
[f761f1eb]177
178 /*
[43114c5]179 * Do CPU usage accounting and find out whether to preempt THREAD.
[f761f1eb]180 */
181
[43114c5]182 if (THREAD) {
[7f1c620]183 uint64_t ticks;
[dbe9ff0]184
[43114c5]185 spinlock_lock(&CPU->lock);
[8cf8ee6]186 CPU->needs_relink += 1 + missed_clock_ticks;
[43114c5]187 spinlock_unlock(&CPU->lock);
[f761f1eb]188
[43114c5]189 spinlock_lock(&THREAD->lock);
[8cf8ee6]190 if ((ticks = THREAD->ticks)) {
191 if (ticks >= 1 + missed_clock_ticks)
192 THREAD->ticks -= 1 + missed_clock_ticks;
193 else
194 THREAD->ticks = 0;
195 }
[dbe9ff0]196 spinlock_unlock(&THREAD->lock);
197
198 if (!ticks && !PREEMPTION_DISABLED) {
[5d9430d7]199#ifdef CONFIG_UDEBUG
200 istate_t *istate;
201#endif
[f761f1eb]202 scheduler();
[3ff2b54]203#ifdef CONFIG_UDEBUG
204 /*
205 * Give udebug chance to stop the thread
[5d9430d7]206 * before it begins executing userspace code.
[3ff2b54]207 */
[5d9430d7]208 istate = THREAD->udebug.uspace_state;
209 if (istate && istate_from_uspace(istate))
[3ff2b54]210 udebug_before_thread_runs();
211#endif
[f761f1eb]212 }
213 }
214
215}
[b45c443]216
[1bb2e7a]217/** @}
[b45c443]218 */
Note: See TracBrowser for help on using the repository browser.