1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2003 Cluster File Systems, Inc.
5 * Author: Eric Barton <eeb@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 # define EXPORT_SYMTAB
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/kmod.h>
30 #include <linux/kernel.h>
31 #include <linux/kernel.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/smp_lock.h>
37 #include <linux/unistd.h>
38 #include <linux/interrupt.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
42 #define DEBUG_SUBSYSTEM S_PORTALS
44 #include <linux/kp30.h>
49 int lwt_pages_per_cpu;
50 lwt_cpu_t lwt_cpus[LWT_MAX_CPUS];
52 /* NB only root is allowed to retrieve LWT info; it's an open door into the
56 lwt_lookup_string (int *size, char *knl_ptr,
57 char *user_ptr, int user_size)
61 /* knl_ptr was retrieved from an LWT snapshot and the caller wants to
62 * turn it into a string. NB we can crash with an access violation
63 * trying to determine the string length, so we're trusting our
66 if (!capable(CAP_SYS_ADMIN))
73 *size = strnlen (knl_ptr, maxsize - 1) + 1;
75 if (user_ptr != NULL) {
79 if (copy_to_user (user_ptr, knl_ptr, *size))
82 /* Did I truncate the string? */
83 if (knl_ptr[*size - 1] != 0)
84 copy_to_user (user_ptr + *size - 4, "...", 4);
91 lwt_control (int enable, int clear)
97 if (!capable(CAP_SYS_ADMIN))
101 for (i = 0; i < num_online_cpus(); i++) {
102 p = lwt_cpus[i].lwtc_current_page;
104 for (j = 0; j < lwt_pages_per_cpu; j++) {
105 memset (p->lwtp_events, 0, PAGE_SIZE);
107 p = list_entry (p->lwtp_list.next,
108 lwt_page_t, lwtp_list);
112 lwt_enabled = enable;
115 /* give people some time to stop adding traces */
116 schedule_timeout(10);
123 lwt_snapshot (cycles_t *now, int *ncpu, int *total_size,
124 void *user_ptr, int user_size)
126 const int events_per_page = PAGE_SIZE / sizeof(lwt_event_t);
127 const int bytes_per_page = events_per_page * sizeof(lwt_event_t);
132 if (!capable(CAP_SYS_ADMIN))
135 *ncpu = num_online_cpus();
136 *total_size = num_online_cpus() * lwt_pages_per_cpu * bytes_per_page;
139 if (user_ptr == NULL)
142 for (i = 0; i < num_online_cpus(); i++) {
143 p = lwt_cpus[i].lwtc_current_page;
145 for (j = 0; j < lwt_pages_per_cpu; j++) {
146 if (copy_to_user(user_ptr, p->lwtp_events,
150 user_ptr = ((char *)user_ptr) + bytes_per_page;
151 p = list_entry(p->lwtp_list.next,
152 lwt_page_t, lwtp_list);
166 if (num_online_cpus() > LWT_MAX_CPUS) {
167 CERROR ("Too many CPUs\n");
171 /* NULL pointers, zero scalars */
172 memset (lwt_cpus, 0, sizeof (lwt_cpus));
173 lwt_pages_per_cpu = LWT_MEMORY / (num_online_cpus() * PAGE_SIZE);
175 for (i = 0; i < num_online_cpus(); i++)
176 for (j = 0; j < lwt_pages_per_cpu; j++) {
177 struct page *page = alloc_page (GFP_KERNEL);
181 CERROR ("Can't allocate page\n");
186 PORTAL_ALLOC(lwtp, sizeof (*lwtp));
188 CERROR ("Can't allocate lwtp\n");
194 lwtp->lwtp_page = page;
195 lwtp->lwtp_events = page_address(page);
196 memset (lwtp->lwtp_events, 0, PAGE_SIZE);
199 INIT_LIST_HEAD (&lwtp->lwtp_list);
200 lwt_cpus[i].lwtc_current_page = lwtp;
202 list_add (&lwtp->lwtp_list,
203 &lwt_cpus[i].lwtc_current_page->lwtp_list);
218 if (num_online_cpus() > LWT_MAX_CPUS)
221 for (i = 0; i < num_online_cpus(); i++)
222 while (lwt_cpus[i].lwtc_current_page != NULL) {
223 lwt_page_t *lwtp = lwt_cpus[i].lwtc_current_page;
225 if (list_empty (&lwtp->lwtp_list)) {
226 lwt_cpus[i].lwtc_current_page = NULL;
228 lwt_cpus[i].lwtc_current_page =
229 list_entry (lwtp->lwtp_list.next,
230 lwt_page_t, lwtp_list);
232 list_del (&lwtp->lwtp_list);
235 __free_page (lwtp->lwtp_page);
236 PORTAL_FREE (lwtp, sizeof (*lwtp));
240 EXPORT_SYMBOL(lwt_enabled);
241 EXPORT_SYMBOL(lwt_cpus);
243 EXPORT_SYMBOL(lwt_init);
244 EXPORT_SYMBOL(lwt_fini);
245 EXPORT_SYMBOL(lwt_lookup_string);
246 EXPORT_SYMBOL(lwt_control);
247 EXPORT_SYMBOL(lwt_snapshot);