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/heap.h>
39 #include <vppinfra/pool.h>
40 #include <vppinfra/format.h>
47 * format_all_svm_regions
48 * Maps / unmaps regions. Do NOT call from client code!
51 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
76 pool_foreach (subp, mp->subregions, ({
77 name = vec_dup (subp->subregion_name);
78 vec_add1(svm_names, name);
82 pthread_mutex_unlock (&root_rp->mutex);
84 for (i = 0; i < vec_len (svm_names); i++)
87 a->name = (char *) svm_names[i];
88 rp = svm_region_find_or_create (a);
91 pthread_mutex_lock (&rp->mutex);
92 s = format (s, "%U", format_svm_region, rp, verbose);
93 pthread_mutex_unlock (&rp->mutex);
94 svm_region_unmap (rp);
95 vec_free (svm_names[i]);
104 show (char *chroot_path, int verbose)
106 svm_map_region_args_t *a = 0;
110 svm_region_init_chroot (chroot_path);
112 fformat (stdout, "My pid is %d\n", getpid ());
114 fformat (stdout, "%U", format_all_svm_regions, verbose);
123 svm_map_region_nolock (svm_map_region_args_t * a)
130 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
132 shm_name = shm_name_from_svm_map_region_args (a);
134 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
138 perror ("svm_region_map(mmap open)");
143 rp = mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
145 if (rp == (svm_region_t *) MAP_FAILED)
148 clib_warning ("mmap");
152 * We lost the footrace to create this region; make sure
153 * the winner has crossed the finish line.
155 while (rp->version == 0 && deadman++ < 5)
163 if (rp->version == 0)
165 clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
166 munmap (rp, MMAP_PAGESIZE);
169 /* Remap now that the region has been placed */
170 a->baseva = rp->virtual_base;
171 a->size = rp->virtual_size;
172 munmap (rp, MMAP_PAGESIZE);
174 rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
175 PROT_READ | PROT_WRITE,
176 MAP_SHARED | MAP_FIXED, svm_fd, 0);
177 if ((uword) rp == (uword) MAP_FAILED)
179 clib_unix_warning ("mmap");
183 if ((uword) rp != rp->virtual_base)
185 clib_warning ("mmap botch");
188 if (pthread_mutex_trylock (&rp->mutex))
190 clib_warning ("rp->mutex LOCKED by pid %d, tag %d, cleared...",
191 rp->mutex_owner_pid, rp->mutex_owner_tag);
192 clib_memset (&rp->mutex, 0, sizeof (rp->mutex));
197 clib_warning ("mutex OK...\n");
198 pthread_mutex_unlock (&rp->mutex);
201 return ((void *) rp);
206 * Round to a pagesize multiple, presumably 4k works
209 rnd_pagesize (u64 size)
213 rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
220 region_lock (svm_region_t * rp, int tag)
222 pthread_mutex_lock (&rp->mutex);
224 rp->mutex_owner_pid = getpid ();
225 rp->mutex_owner_tag = tag;
230 region_unlock (svm_region_t * rp)
233 rp->mutex_owner_pid = 0;
234 rp->mutex_owner_tag = 0;
236 pthread_mutex_unlock (&rp->mutex);
241 svm_existing_region_map_nolock (void *root_arg, svm_map_region_args_t * a)
243 svm_region_t *root_rp = root_arg;
244 svm_main_region_t *mp;
249 a->size += MMAP_PAGESIZE +
250 (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
251 a->size = rnd_pagesize (a->size);
253 region_lock (root_rp, 4);
254 oldheap = svm_push_pvt_heap (root_rp);
255 mp = root_rp->data_base;
259 p = hash_get_mem (mp->name_hash, a->name);
263 rp = svm_map_region_nolock (a);
264 region_unlock (root_rp);
265 svm_pop_heap (oldheap);
273 trace (char *chroot_path, char *name, int enable_disable)
275 svm_map_region_args_t *a = 0;
281 svm_region_init_chroot (chroot_path);
285 a->flags = SVM_FLAGS_MHEAP;
287 db_rp = svm_region_find_or_create (a);
291 region_lock (db_rp, 20);
293 oldheap = svm_push_data_heap (db_rp);
295 mheap_trace (db_rp->data_heap, enable_disable);
297 svm_pop_heap (oldheap);
298 region_unlock (db_rp);
300 svm_region_unmap ((void *) db_rp);
308 subregion_repair (char *chroot_path)
311 svm_main_region_t *mp;
312 svm_map_region_args_t a;
313 svm_region_t *root_rp;
315 svm_subregion_t *subp;
319 svm_region_init_chroot (chroot_path);
320 root_rp = svm_get_root_rp ();
322 pthread_mutex_lock (&root_rp->mutex);
324 mp = root_rp->data_base;
327 * Snapshoot names, can't hold root rp mutex across
331 pool_foreach (subp, mp->subregions, ({
332 name = vec_dup (subp->subregion_name);
333 vec_add1(svm_names, name);
337 pthread_mutex_unlock (&root_rp->mutex);
339 for (i = 0; i < vec_len (svm_names); i++)
341 clib_memset (&a, 0, sizeof (a));
342 a.root_path = chroot_path;
343 a.name = (char *) svm_names[i];
344 fformat (stdout, "Checking %s region...\n", a.name);
345 rp = svm_existing_region_map_nolock (root_rp, &a);
348 svm_region_unmap (rp);
349 vec_free (svm_names[i]);
352 vec_free (svm_names);
356 repair (char *chroot_path, int crash_root_region)
358 svm_region_t *root_rp = 0;
359 svm_map_region_args_t *a = 0;
360 void *svm_map_region (svm_map_region_args_t * a);
364 fformat (stdout, "our pid: %d\n", getpid ());
368 a->root_path = chroot_path;
369 a->name = SVM_GLOBAL_REGION_NAME;
370 a->baseva = svm_get_global_region_base_va ();
371 a->size = SVM_GLOBAL_REGION_SIZE;
372 a->flags = SVM_FLAGS_NODATA;
374 shm_name = shm_name_from_svm_map_region_args (a);
376 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
380 perror ("svm_region_map(mmap open)");
386 root_rp = mmap (0, MMAP_PAGESIZE,
387 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
389 if (root_rp == (svm_region_t *) MAP_FAILED)
392 clib_warning ("mmap");
396 /* Remap now that the region has been placed */
397 clib_warning ("remap to 0x%x", root_rp->virtual_base);
399 a->baseva = root_rp->virtual_base;
400 a->size = root_rp->virtual_size;
401 munmap (root_rp, MMAP_PAGESIZE);
403 root_rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
404 PROT_READ | PROT_WRITE,
405 MAP_SHARED | MAP_FIXED, svm_fd, 0);
406 if ((uword) root_rp == (uword) MAP_FAILED)
408 clib_unix_warning ("mmap");
414 if ((uword) root_rp != root_rp->virtual_base)
416 clib_warning ("mmap botch");
420 if (pthread_mutex_trylock (&root_rp->mutex))
422 clib_warning ("root_rp->mutex LOCKED by pid %d, tag %d, cleared...",
423 root_rp->mutex_owner_pid, root_rp->mutex_owner_tag);
424 clib_memset (&root_rp->mutex, 0, sizeof (root_rp->mutex));
429 clib_warning ("root_rp->mutex OK...\n");
430 pthread_mutex_unlock (&root_rp->mutex);
436 * Now that the root region is known to be OK,
437 * fix broken subregions
439 subregion_repair (chroot_path);
441 if (crash_root_region)
443 clib_warning ("Leaving root region locked on purpose...");
444 pthread_mutex_lock (&root_rp->mutex);
445 root_rp->mutex_owner_pid = getpid ();
446 root_rp->mutex_owner_tag = 99;
452 main (int argc, char **argv)
454 unformat_input_t input;
457 char *chroot_path = 0;
460 clib_mem_init_thread_safe (0, 128 << 20);
462 unformat_init_command_line (&input, argv);
464 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
466 if (unformat (&input, "show-verbose"))
468 show (chroot_path, 1);
471 else if (unformat (&input, "show"))
473 show (chroot_path, 0);
476 else if (unformat (&input, "client-scan"))
478 svm_client_scan (chroot_path);
481 else if (unformat (&input, "repair"))
483 repair (chroot_path, 0 /* fix it */ );
486 else if (unformat (&input, "crash"))
488 repair (chroot_path, 1 /* crash it */ );
491 else if (unformat (&input, "trace-on %s", &name))
493 trace (chroot_path, name, 1);
496 else if (unformat (&input, "trace-off %s", &name))
498 trace (chroot_path, name, 0);
501 else if (unformat (&input, "chroot %s", &chroot_u8))
503 chroot_path = (char *) chroot_u8;
511 unformat_free (&input);
516 "%s: show | show-verbose | client-scan | trace-on <region-name>\n",
518 fformat (stdout, " trace-off <region-name>\n");
524 * fd.io coding-style-patch-verification: ON
527 * eval: (c-set-style "gnu")