Debian Codegraph @

test/dapltest/mdep/linux/dapl_mdep_kernel.c

raw
1 /*
2 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
3 *
4 * This Software is licensed under one of the following licenses:
5 *
6 * 1) under the terms of the "Common Public License 1.0" a copy of which is
7 * in the file LICENSE.txt in the root directory. The license is also
8 * available from the Open Source Initiative, see
9 * http://www.opensource.org/licenses/cpl.php.
10 *
11 * 2) under the terms of the "The BSD License" a copy of which is in the file
12 * LICENSE2.txt in the root directory. The license is also available from
13 * the Open Source Initiative, see
14 * http://www.opensource.org/licenses/bsd-license.php.
15 *
16 * 3) under the terms of the "GNU General Public License (GPL) Version 2" a
17 * copy of which is in the file LICENSE3.txt in the root directory. The
18 * license is also available from the Open Source Initiative, see
19 * http://www.opensource.org/licenses/gpl-license.php.
20 *
21 * Licensee has the right to choose one of the above licenses.
22 *
23 * Redistributions of source code must retain the above copyright
24 * notice and one of the license notices.
25 *
26 * Redistributions in binary form must reproduce both the above copyright
27 * notice, one of the license notices in the documentation
28 * and/or other materials provided with the distribution.
29 */
30
31 #include "dapl_proto.h"
32 #include "kdapl_tdep.h"
33 #include <linux/slab.h>
34 #ifndef UPCALL_FROM_IRQ
35 #include <linux/interrupt.h> /* for spin_lock_bh */
36 #endif /* UPCALL_FROM_IRQ */
37
38 #include <linux/kernel_stat.h> /* Getting Statistics */
39 #include <linux/smp.h> /* Definitions of smp_num_cpus and cpu_logical_map(cpu) */
40 #include <linux/sched.h> /* jiffies */
41 #include <linux/version.h>
42
43 /*
44 * Sleep specified number of milliseconds
45 */
46
47 void DT_Mdep_Sleep(int msec)
48 {
49 set_current_state(TASK_INTERRUPTIBLE);
50 schedule_timeout(msec * HZ / 1000);
51 }
52
53 void DT_Mdep_Schedule(void)
54 {
55 schedule();
56 }
57
58 /*
59 * Get system statistics including uptime and idle time
60 */
61
62 bool DT_Mdep_GetCpuStat(DT_CpuStat * cpu_stat)
63 {
64 unsigned long i, nice = 0;
65 unsigned long jif = jiffies;
66 int cpu, num_of_cpus;
67 cpu_stat->user = 0;
68 cpu_stat->system = 0;
69 cpu_stat->idle = 0;
70 num_of_cpus = 0;
71 cpu = 0;
72
73 // code for vanila 2.4 kernel
74 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && !defined RHAS3
75 for (i = 0; i < smp_num_cpus; i++) {
76 cpu = cpu_logical_map(i);
77 cpu_stat->user += (unsigned long)kstat.per_cpu_user[cpu];
78 nice += (unsigned long)kstat.per_cpu_nice[cpu];
79 cpu_stat->system += (unsigned long)kstat.per_cpu_system[cpu];
80 }
81 // kernel 2.4 AS3.0
82 #elif (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && defined RHAS3
83 cpu_stat->user = kstat_sum(accumulated_time.u_usec);
84 cpu_stat->system = kstat_sum(accumulated_time.s_usec);
85 nice = kstat_sum(accumulated_time.n_usec);
86 // kernel 2.6
87 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
88 for (i = 0; i < NR_CPUS; i++) {
89 if (cpu_possible(i)) {
90 cpu_stat->system += kstat_cpu(i).cpustat.system;
91 cpu_stat->user += kstat_cpu(i).cpustat.user;
92 nice += kstat_cpu(i).cpustat.nice;
93 num_of_cpus = +1;
94 }
95 }
96 #endif
97 cpu_stat->idle =
98 (unsigned long)(jif * num_of_cpus -
99 (cpu_stat->user + nice + cpu_stat->system));
100 return true;
101 }
102
103 /*
104 * Get current time in milliseconds (relative to some fixed point)
105 */
106 unsigned long DT_Mdep_GetTime(void)
107 {
108 struct timeval tv;
109
110 do_gettimeofday(&tv);
111 return ((unsigned long)(tv.tv_sec) * 1000L) +
112 (unsigned long)(tv.tv_usec) / 1000;
113 }
114
115 #if 0
116 unsigned long DT_Mdep_GetContextSwitchNum(void)
117 {
118 return nr_context_switches();
119 }
120 #endif
121
122 /*
123 * Memory allocate and free routines for control blocks (objects) - regular
124 * memory, always zeroed.
125 */
126 void *DT_Mdep_Malloc(size_t l_)
127 {
128 void *rval;
129
130 /*
131 * check memory leaking DT_Mdep_Lock(&Alloc_Count_Lock); alloc_count++;
132 * DT_Mdep_Unlock(&Alloc_Count_Lock);
133 */
134
135 rval = kmalloc(l_, GFP_ATOMIC);
136
137 if (rval) {
138 memset(rval, 0, l_);
139 }
140 return (rval);
141 }
142
143 void DT_Mdep_Free(void *a_)
144 {
145 /*
146 * check memory leaking DT_Mdep_Lock(&Alloc_Count_Lock); alloc_count--;
147 * DT_Mdep_Unlock(&Alloc_Count_Lock);
148 */
149
150 kfree(a_);
151 }
152
153 /*
154 * Lock support
155 *
156 * Lock object constructor
157 */
158 bool DT_Mdep_LockInit(DT_Mdep_LockType * lock_ptr)
159 {
160 spin_lock_init(lock_ptr);
161 return true;
162 }
163
164 /*
165 * Lock object destructor
166 */
167 void DT_Mdep_LockDestroy(DT_Mdep_LockType * lock_ptr)
168 {
169 /* nothing */
170 }
171
172 /*
173 * Locks
174 *
175 */
176 void DT_Mdep_Lock(DT_Mdep_LockType * lock_ptr)
177 {
178 SPINLOCK(lock_ptr);
179 }
180
181 /*
182 * unlock
183 */
184 void DT_Mdep_Unlock(DT_Mdep_LockType * lock_ptr)
185 {
186 SPINUNLOCK(lock_ptr);
187 }
188
189 /*
190 * Init Thread Attributes
191 */
192 void DT_Mdep_Thread_Init_Attributes(Thread * thread_ptr)
193 {
194 /* nothing */
195 }
196
197 /*
198 * Destroy Thread Attributes
199 */
200 void DT_Mdep_Thread_Destroy_Attributes(Thread * thread_ptr)
201 {
202 /* nothing */
203 }
204
205 /*
206 * Start the thread
207 */
208 bool DT_Mdep_Thread_Start(Thread * thread_ptr)
209 {
210 thread_ptr->thread_handle = kernel_thread((void *)
211 DT_Mdep_Thread_Start_Routine,
212 (void *)thread_ptr, 0);
213 return (thread_ptr->thread_handle);
214 }
215
216 /*
217 * Thread execution entry point function
218 */
219 DT_Mdep_Thread_Start_Routine_Return_Type
220 DT_Mdep_Thread_Start_Routine(void *thread_handle)
221 {
222 Thread *thread_ptr;
223 thread_ptr = (Thread *) thread_handle;
224 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
225 reparent_to_init();
226 #else
227 daemonize(__func__);
228 #endif
229 thread_ptr->function(thread_ptr->param);
230 return 0;
231 }
232
233 /*
234 * Thread detach routine. Allows the pthreads
235 * interface to clean up resources properly at
236 * thread's end.
237 */
238 void DT_Mdep_Thread_Detach(int thread_id)
239 { /* AMM */
240 /* nothing */
241 }
242
243 /*
244 * Allows a thread to get its own ID so it
245 * can pass it to routines wanting to act
246 * upon themselves.
247 */
248
249 int DT_Mdep_Thread_SELF(void)
250 { /* AMM */
251 /* nothing */
252 return (0);
253 }
254
255 /*
256 * Allow a thread to exit and cleanup resources.
257 */
258
259 void DT_Mdep_Thread_EXIT(void *thread_handle)
260 { /* AMM */
261 /* nothing */
262 }
263
264 /*
265 * DT_Mdep_wait_object_init
266 *
267 * Initialize a wait object
268 *
269 * Input:
270 * wait_obj
271 *
272 * Returns:
273 * 0 if successful
274 * -1 if unsuccessful
275 */
276 int DT_Mdep_wait_object_init(IN DT_WAIT_OBJECT * wait_obj)
277 {
278 init_waitqueue_head(wait_obj);
279 return 0;
280 }
281
282 /* Wait on the supplied wait object, up to the specified time_out.
283 * A timeout of DAT_TIMEOUT_INFINITE will wait indefinitely.
284 * Timeout should be specified in micro seconds.
285 *
286 * Functional returns:
287 * 0 -- another thread invoked dapl_os_wait object_wakeup
288 * -1 -- someone else is already waiting in this wait
289 * object.
290 * only one waiter is allowed at a time.
291 * -1 -- another thread invoked dapl_os_wait_object_destroy
292 * -1 -- the specified time limit was reached.
293 */
294
295 int DT_Mdep_wait_object_wait(IN DT_WAIT_OBJECT * wait_obj, IN int timeout_val)
296 {
297 int expire;
298 int dat_status;
299
300 dat_status = DAT_SUCCESS;
301 if (DAT_TIMEOUT_INFINITE == timeout_val) {
302 interruptible_sleep_on(wait_obj);
303 } else {
304 expire = timeout_val * HZ / 1000000;
305 while (expire) {
306 current->state = TASK_INTERRUPTIBLE;
307 expire = schedule_timeout(expire);
308 }
309 dat_status = DAT_TIMEOUT_EXPIRED;
310 }
311 return dat_status;
312 }
313
314 /*
315 * DT_Mdep_wait_object_wakeup
316 *
317 * Wakeup a thread waiting on a wait object
318 *
319 * Input:
320 * wait_obj
321 *
322 * Returns:
323 * 0 if successful
324 * -1 if not successful
325 */
326 int DT_Mdep_wait_object_wakeup(DT_WAIT_OBJECT * wait_obj)
327 {
328 wake_up_interruptible(wait_obj);
329 return 0;
330 }
331
332 /*
333 * DT_Mdep_wait_object_destroy
334 *
335 * Destroy a wait object
336 *
337 * Input:
338 * wait_obj
339 *
340 * Returns:
341 * 0 if successful
342 * -1 if not successful
343 */
344 int DT_Mdep_wait_object_destroy(IN DT_WAIT_OBJECT * wait_obj)
345 {
346 return 0;
347 }