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!
52 format_all_svm_regions (u8 * s, va_list * args)
54 int verbose = va_arg (*args, int);
55 svm_region_t *root_rp = svm_get_root_rp ();
56 svm_main_region_t *mp;
57 svm_subregion_t *subp;
59 svm_map_region_args_t *a = 0;
66 pthread_mutex_lock (&root_rp->mutex);
68 s = format (s, "%U", format_svm_region, root_rp, verbose);
70 mp = root_rp->data_base;
73 * Snapshoot names, can't hold root rp mutex across
77 pool_foreach (subp, mp->subregions, ({
78 name = vec_dup (subp->subregion_name);
79 vec_add1(svm_names, name);
83 pthread_mutex_unlock (&root_rp->mutex);
85 for (i = 0; i < vec_len (svm_names); i++)
88 a->name = (char *) svm_names[i];
89 rp = svm_region_find_or_create (a);
92 pthread_mutex_lock (&rp->mutex);
93 s = format (s, "%U", format_svm_region, rp, verbose);
94 pthread_mutex_unlock (&rp->mutex);
95 svm_region_unmap (rp);
96 vec_free (svm_names[i]);
100 vec_free (svm_names);
105 show (char *chroot_path, int verbose)
107 svm_map_region_args_t *a = 0;
111 svm_region_init_chroot (chroot_path);
113 fformat (stdout, "My pid is %d\n", getpid ());
115 fformat (stdout, "%U", format_all_svm_regions, verbose);
124 svm_map_region_nolock (svm_map_region_args_t * a)
131 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
133 shm_name = shm_name_from_svm_map_region_args (a);
135 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
139 perror ("svm_region_map(mmap open)");
144 rp = mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
146 if (rp == (svm_region_t *) MAP_FAILED)
149 clib_warning ("mmap");
153 * We lost the footrace to create this region; make sure
154 * the winner has crossed the finish line.
156 while (rp->version == 0 && deadman++ < 5)
164 if (rp->version == 0)
166 clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
167 munmap (rp, MMAP_PAGESIZE);
170 /* Remap now that the region has been placed */
171 a->baseva = rp->virtual_base;
172 a->size = rp->virtual_size;
173 munmap (rp, MMAP_PAGESIZE);
175 rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
176 PROT_READ | PROT_WRITE,
177 MAP_SHARED | MAP_FIXED, svm_fd, 0);
178 if ((uword) rp == (uword) MAP_FAILED)
180 clib_unix_warning ("mmap");
184 if ((uword) rp != rp->virtual_base)
186 clib_warning ("mmap botch");
189 if (pthread_mutex_trylock (&rp->mutex))
191 clib_warning ("rp->mutex LOCKED by pid %d, tag %d, cleared...",
192 rp->mutex_owner_pid, rp->mutex_owner_tag);
193 memset (&rp->mutex, 0, sizeof (rp->mutex));
198 clib_warning ("mutex OK...\n");
199 pthread_mutex_unlock (&rp->mutex);
202 return ((void *) rp);
207 * Round to a pagesize multiple, presumably 4k works
210 rnd_pagesize (u64 size)
214 rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
221 region_lock (svm_region_t * rp, int tag)
223 pthread_mutex_lock (&rp->mutex);
225 rp->mutex_owner_pid = getpid ();
226 rp->mutex_owner_tag = tag;
231 region_unlock (svm_region_t * rp)
234 rp->mutex_owner_pid = 0;
235 rp->mutex_owner_tag = 0;
237 pthread_mutex_unlock (&rp->mutex);
242 svm_existing_region_map_nolock (void *root_arg, svm_map_region_args_t * a)
244 svm_region_t *root_rp = root_arg;
245 svm_main_region_t *mp;
250 a->size += MMAP_PAGESIZE +
251 (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
252 a->size = rnd_pagesize (a->size);
254 region_lock (root_rp, 4);
255 oldheap = svm_push_pvt_heap (root_rp);
256 mp = root_rp->data_base;
260 p = hash_get_mem (mp->name_hash, a->name);
264 rp = svm_map_region_nolock (a);
265 region_unlock (root_rp);
266 svm_pop_heap (oldheap);
274 trace (char *chroot_path, char *name, int enable_disable)
276 svm_map_region_args_t *a = 0;
282 svm_region_init_chroot (chroot_path);
286 a->flags = SVM_FLAGS_MHEAP;
288 db_rp = svm_region_find_or_create (a);
292 region_lock (db_rp, 20);
294 oldheap = svm_push_data_heap (db_rp);
296 mheap_trace (db_rp->data_heap, enable_disable);
298 svm_pop_heap (oldheap);
299 region_unlock (db_rp);
301 svm_region_unmap ((void *) db_rp);
309 subregion_repair (char *chroot_path)
312 svm_main_region_t *mp;
313 svm_map_region_args_t a;
314 svm_region_t *root_rp;
316 svm_subregion_t *subp;
320 svm_region_init_chroot (chroot_path);
321 root_rp = svm_get_root_rp ();
323 pthread_mutex_lock (&root_rp->mutex);
325 mp = root_rp->data_base;
328 * Snapshoot names, can't hold root rp mutex across
332 pool_foreach (subp, mp->subregions, ({
333 name = vec_dup (subp->subregion_name);
334 vec_add1(svm_names, name);
338 pthread_mutex_unlock (&root_rp->mutex);
340 for (i = 0; i < vec_len (svm_names); i++)
342 memset (&a, 0, sizeof (a));
343 a.root_path = chroot_path;
344 a.name = (char *) svm_names[i];
345 fformat (stdout, "Checking %s region...\n", a.name);
346 rp = svm_existing_region_map_nolock (root_rp, &a);
349 svm_region_unmap (rp);
350 vec_free (svm_names[i]);
353 vec_free (svm_names);
357 repair (char *chroot_path, int crash_root_region)
359 svm_region_t *root_rp = 0;
360 svm_map_region_args_t *a = 0;
361 void *svm_map_region (svm_map_region_args_t * a);
365 fformat (stdout, "our pid: %d\n", getpid ());
369 a->root_path = chroot_path;
370 a->name = SVM_GLOBAL_REGION_NAME;
371 a->baseva = svm_get_global_region_base_va ();
372 a->size = SVM_GLOBAL_REGION_SIZE;
373 a->flags = SVM_FLAGS_NODATA;
375 shm_name = shm_name_from_svm_map_region_args (a);
377 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
381 perror ("svm_region_map(mmap open)");
387 root_rp = mmap (0, MMAP_PAGESIZE,
388 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
390 if (root_rp == (svm_region_t *) MAP_FAILED)
393 clib_warning ("mmap");
397 /* Remap now that the region has been placed */
398 clib_warning ("remap to 0x%x", root_rp->virtual_base);
400 a->baseva = root_rp->virtual_base;
401 a->size = root_rp->virtual_size;
402 munmap (root_rp, MMAP_PAGESIZE);
404 root_rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
405 PROT_READ | PROT_WRITE,
406 MAP_SHARED | MAP_FIXED, svm_fd, 0);
407 if ((uword) root_rp == (uword) MAP_FAILED)
409 clib_unix_warning ("mmap");
415 if ((uword) root_rp != root_rp->virtual_base)
417 clib_warning ("mmap botch");
421 if (pthread_mutex_trylock (&root_rp->mutex))
423 clib_warning ("root_rp->mutex LOCKED by pid %d, tag %d, cleared...",
424 root_rp->mutex_owner_pid, root_rp->mutex_owner_tag);
425 memset (&root_rp->mutex, 0, sizeof (root_rp->mutex));
430 clib_warning ("root_rp->mutex OK...\n");
431 pthread_mutex_unlock (&root_rp->mutex);
437 * Now that the root region is known to be OK,
438 * fix broken subregions
440 subregion_repair (chroot_path);
442 if (crash_root_region)
444 clib_warning ("Leaving root region locked on purpose...");
445 pthread_mutex_lock (&root_rp->mutex);
446 root_rp->mutex_owner_pid = getpid ();
447 root_rp->mutex_owner_tag = 99;
453 main (int argc, char **argv)
455 unformat_input_t input;
458 char *chroot_path = 0;
461 unformat_init_command_line (&input, argv);
463 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
465 if (unformat (&input, "show-verbose"))
467 show (chroot_path, 1);
470 else if (unformat (&input, "show"))
472 show (chroot_path, 0);
475 else if (unformat (&input, "client-scan"))
477 svm_client_scan (chroot_path);
480 else if (unformat (&input, "repair"))
482 repair (chroot_path, 0 /* fix it */ );
485 else if (unformat (&input, "crash"))
487 repair (chroot_path, 1 /* crash it */ );
490 else if (unformat (&input, "trace-on %s", &name))
492 trace (chroot_path, name, 1);
495 else if (unformat (&input, "trace-off %s", &name))
497 trace (chroot_path, name, 0);
500 else if (unformat (&input, "chroot %s", &chroot_u8))
502 chroot_path = (char *) chroot_u8;
510 unformat_free (&input);
515 "%s: show | show-verbose | client-scan | trace-on <region-name>\n",
517 fformat (stdout, " trace-off <region-name>\n");
523 * fd.io coding-style-patch-verification: ON
526 * eval: (c-set-style "gnu")