2 *------------------------------------------------------------------
5 * Copyright (c) 2009 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
22 #include <sys/types.h>
25 #include <netinet/in.h>
32 #include <vppinfra/clib.h>
33 #include <vppinfra/vec.h>
34 #include <vppinfra/hash.h>
35 #include <vppinfra/bitmap.h>
36 #include <vppinfra/fifo.h>
37 #include <vppinfra/time.h>
38 #include <vppinfra/mheap.h>
39 #include <vppinfra/heap.h>
40 #include <vppinfra/pool.h>
41 #include <vppinfra/format.h>
48 * format_all_svm_regions
49 * Maps / unmaps regions. Do NOT call from client code!
51 u8 *format_all_svm_regions (u8 *s, va_list * args)
53 int verbose = va_arg (*args, int);
54 svm_region_t *root_rp = svm_get_root_rp();
55 svm_main_region_t *mp;
56 svm_subregion_t *subp;
58 svm_map_region_args_t *a = 0;
65 pthread_mutex_lock (&root_rp->mutex);
67 s = format (s, "%U", format_svm_region, root_rp, verbose);
69 mp = root_rp->data_base;
72 * Snapshoot names, can't hold root rp mutex across
75 pool_foreach (subp, mp->subregions, ({
76 name = vec_dup (subp->subregion_name);
77 vec_add1(svm_names, name);
80 pthread_mutex_unlock (&root_rp->mutex);
82 for (i = 0; i < vec_len(svm_names); i++) {
84 a->name = (char *) svm_names[i];
85 rp = svm_region_find_or_create (a);
87 pthread_mutex_lock (&rp->mutex);
88 s = format (s, "%U", format_svm_region, rp, verbose);
89 pthread_mutex_unlock (&rp->mutex);
90 svm_region_unmap (rp);
91 vec_free(svm_names[i]);
99 void show (char *chroot_path, int verbose)
101 svm_map_region_args_t *a = 0;
105 svm_region_init_chroot(chroot_path);
107 fformat(stdout, "My pid is %d\n", getpid());
109 fformat(stdout, "%U", format_all_svm_regions, verbose);
117 static void *svm_map_region_nolock (svm_map_region_args_t *a)
124 ASSERT((a->size & ~(MMAP_PAGESIZE-1)) == a->size);
126 shm_name = shm_name_from_svm_map_region_args (a);
128 svm_fd = shm_open((char *)shm_name, O_RDWR, 0777);
131 perror("svm_region_map(mmap open)");
136 rp = mmap(0, MMAP_PAGESIZE,
137 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
139 if (rp == (svm_region_t *) MAP_FAILED) {
141 clib_warning("mmap");
145 * We lost the footrace to create this region; make sure
146 * the winner has crossed the finish line.
148 while (rp->version == 0 && deadman++ < 5) {
155 if (rp->version == 0) {
156 clib_warning("rp->version %d not %d", rp->version,
160 /* Remap now that the region has been placed */
161 a->baseva = rp->virtual_base;
162 a->size = rp->virtual_size;
163 munmap(rp, MMAP_PAGESIZE);
165 rp = (void *) mmap ((void *)a->baseva, a->size,
166 PROT_READ | PROT_WRITE,
167 MAP_SHARED | MAP_FIXED, svm_fd, 0);
168 if ((uword)rp == (uword)MAP_FAILED) {
169 clib_unix_warning ("mmap");
173 if ((uword) rp != rp->virtual_base) {
174 clib_warning("mmap botch");
177 if (pthread_mutex_trylock(&rp->mutex)) {
178 clib_warning ("rp->mutex LOCKED by pid %d, tag %d, cleared...",
179 rp->mutex_owner_pid, rp->mutex_owner_tag);
180 memset(&rp->mutex, 0, sizeof (rp->mutex));
183 clib_warning ("mutex OK...\n");
184 pthread_mutex_unlock(&rp->mutex);
187 return ((void *) rp);
192 * Round to a pagesize multiple, presumably 4k works
194 static unsigned int rnd_pagesize(unsigned int size)
198 rv = (size + (MMAP_PAGESIZE-1)) & ~(MMAP_PAGESIZE-1);
204 always_inline void region_lock(svm_region_t *rp, int tag)
206 pthread_mutex_lock(&rp->mutex);
208 rp->mutex_owner_pid = getpid();
209 rp->mutex_owner_tag = tag;
213 always_inline void region_unlock(svm_region_t *rp)
216 rp->mutex_owner_pid = 0;
217 rp->mutex_owner_tag = 0;
219 pthread_mutex_unlock(&rp->mutex);
223 static void *svm_existing_region_map_nolock (void *root_arg,
224 svm_map_region_args_t *a)
226 svm_region_t *root_rp = root_arg;
227 svm_main_region_t *mp;
232 a->size += MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE;
233 a->size = rnd_pagesize(a->size);
235 region_lock (root_rp, 4);
236 oldheap = svm_push_pvt_heap(root_rp);
237 mp = root_rp->data_base;
241 p = hash_get_mem (mp->name_hash, a->name);
244 rp = svm_map_region_nolock (a);
245 region_unlock(root_rp);
246 svm_pop_heap (oldheap);
253 static void trace (char *chroot_path, char *name, int enable_disable)
255 svm_map_region_args_t *a = 0;
261 svm_region_init_chroot(chroot_path);
265 a->flags = SVM_FLAGS_MHEAP;
267 db_rp = svm_region_find_or_create (a);
271 region_lock (db_rp, 20);
273 oldheap = svm_push_data_heap (db_rp);
275 mheap_trace (db_rp->data_heap, enable_disable);
277 svm_pop_heap (oldheap);
278 region_unlock (db_rp);
280 svm_region_unmap ((void *)db_rp);
287 static void subregion_repair(char *chroot_path)
290 svm_main_region_t *mp;
291 svm_map_region_args_t a;
292 svm_region_t *root_rp;
294 svm_subregion_t *subp;
298 svm_region_init_chroot(chroot_path);
299 root_rp = svm_get_root_rp();
301 pthread_mutex_lock (&root_rp->mutex);
303 mp = root_rp->data_base;
306 * Snapshoot names, can't hold root rp mutex across
309 pool_foreach (subp, mp->subregions, ({
310 name = vec_dup (subp->subregion_name);
311 vec_add1(svm_names, name);
314 pthread_mutex_unlock (&root_rp->mutex);
316 for (i = 0; i < vec_len(svm_names); i++) {
317 memset (&a, 0, sizeof (a));
318 a.root_path = chroot_path;
319 a.name = (char *) svm_names[i];
320 fformat(stdout, "Checking %s region...\n",
322 rp = svm_existing_region_map_nolock (root_rp, &a);
324 svm_region_unmap (rp);
325 vec_free(svm_names[i]);
331 void repair (char *chroot_path, int crash_root_region)
333 svm_region_t *root_rp = 0;
334 svm_map_region_args_t *a = 0;
335 void *svm_map_region (svm_map_region_args_t *a);
339 fformat(stdout, "our pid: %d\n", getpid());
343 a->root_path = chroot_path;
344 a->name = SVM_GLOBAL_REGION_NAME;
345 a->baseva = SVM_GLOBAL_REGION_BASEVA;
346 a->size = SVM_GLOBAL_REGION_SIZE;
347 a->flags = SVM_FLAGS_NODATA;
349 shm_name = shm_name_from_svm_map_region_args (a);
351 svm_fd = shm_open ((char *)shm_name, O_RDWR, 0777);
354 perror("svm_region_map(mmap open)");
360 root_rp = mmap(0, MMAP_PAGESIZE,
361 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
363 if (root_rp == (svm_region_t *) MAP_FAILED) {
365 clib_warning("mmap");
369 /* Remap now that the region has been placed */
370 clib_warning ("remap to 0x%x", root_rp->virtual_base);
372 a->baseva = root_rp->virtual_base;
373 a->size = root_rp->virtual_size;
374 munmap(root_rp, MMAP_PAGESIZE);
376 root_rp = (void *) mmap ((void *)a->baseva, a->size,
377 PROT_READ | PROT_WRITE,
378 MAP_SHARED | MAP_FIXED, svm_fd, 0);
379 if ((uword)root_rp == (uword)MAP_FAILED) {
380 clib_unix_warning ("mmap");
386 if ((uword) root_rp != root_rp->virtual_base) {
387 clib_warning("mmap botch");
391 if (pthread_mutex_trylock(&root_rp->mutex)) {
392 clib_warning ("root_rp->mutex LOCKED by pid %d, tag %d, cleared...",
393 root_rp->mutex_owner_pid, root_rp->mutex_owner_tag);
394 memset(&root_rp->mutex, 0, sizeof (root_rp->mutex));
397 clib_warning ("root_rp->mutex OK...\n");
398 pthread_mutex_unlock(&root_rp->mutex);
404 * Now that the root region is known to be OK,
405 * fix broken subregions
407 subregion_repair(chroot_path);
409 if (crash_root_region) {
410 clib_warning ("Leaving root region locked on purpose...");
411 pthread_mutex_lock(&root_rp->mutex);
412 root_rp->mutex_owner_pid = getpid();
413 root_rp->mutex_owner_tag = 99;
418 int main (int argc, char **argv)
420 unformat_input_t input;
423 char *chroot_path = 0;
426 unformat_init_command_line (&input, argv);
428 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) {
429 if (unformat(&input, "show-verbose")) {
430 show (chroot_path, 1);
432 } else if (unformat (&input, "show")) {
433 show (chroot_path, 0);
435 } else if (unformat (&input, "client-scan")) {
436 svm_client_scan(chroot_path);
438 } else if (unformat (&input, "repair")) {
439 repair(chroot_path, 0 /* fix it */);
441 } else if (unformat (&input, "crash")) {
442 repair (chroot_path, 1 /* crash it */);
444 } else if (unformat (&input, "trace-on %s", &name)) {
445 trace (chroot_path, name, 1);
447 } else if (unformat (&input, "trace-off %s", &name)) {
448 trace (chroot_path, name, 0);
450 } else if (unformat (&input, "chroot %s", &chroot_u8)) {
451 chroot_path = (char *) chroot_u8;
457 unformat_free (&input);
460 fformat(stdout, "%s: show | show-verbose | client-scan | trace-on <region-name>\n", argv[0]);
461 fformat(stdout, " trace-off <region-name>\n");