23 #include <sys/types.h> 26 #include <netinet/in.h> 64 pthread_mutex_lock (&rp->
mutex);
87 for (i =
nheld - 1; i >= 0; i--)
91 for (j = i; j <
MAXLOCK - 1; j++)
101 pthread_mutex_unlock (&rp->
mutex);
115 s =
format (s,
"NODATA ");
127 if (size >= (1 << 20))
129 s =
format (s,
"(%d mb)", size >> 20);
131 else if (size >= (1 << 10))
133 s =
format (s,
"(%d kb)", size >> 10);
137 s =
format (s,
"(%d bytes)", size);
146 int verbose = va_arg (*args,
int);
150 s =
format (s,
"%s: base va 0x%x size 0x%x %U\n",
153 s =
format (s,
" user_ctx 0x%x, bitmap_size %d\n",
161 " region_heap 0x%x data_base 0x%x data_heap 0x%x\n",
176 s =
format (s,
" VM in use: ");
196 s =
format (s,
" 0x%x - 0x%x (%dk)\n", lo, hi,
254 if (fstat (fd, &statb) < 0)
261 if (statb.st_mode & S_IFREG)
263 if (statb.st_size == 0)
265 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
271 if (write (fd, &junk, 1) != 1)
288 ASSERT (map_size <= rp->virtual_size -
291 if (mmap (rp->
data_base, map_size, PROT_READ | PROT_WRITE,
292 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
336 if (fstat (fd, &statb) < 0)
343 if (statb.st_mode & S_IFREG)
345 if (statb.st_size == 0)
347 if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1)
353 if (write (fd, &junk, 1) != 1)
370 ASSERT (map_size <= rp->virtual_size
375 if (mmap (rp->
data_base, map_size, PROT_READ | PROT_WRITE,
376 MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED)
394 int root_path_offset = 0;
406 path =
format (0,
"/dev/shm/%s%c", &a->
root_path[root_path_offset], 0);
407 split_point = path + 1;
412 while (*split_point && *split_point !=
'/')
420 mkdir_arg[
vec_len (mkdir_arg) - 1] =
'-';
426 if (a->
name[0] ==
'/')
430 &a->
name[name_offset], 0);
445 pthread_mutexattr_t attr;
446 pthread_condattr_t cattr;
453 int nbits, words, bit;
454 int pid_holding_region_lock;
456 int dead_region_recovery = 0;
459 struct timespec ts, tsrem;
469 svm_fd = shm_open ((
char *) shm_name, O_RDWR | O_CREAT | O_EXCL, 0777);
473 if (fchmod (svm_fd, 0770) < 0)
476 if (fchown (svm_fd, a->
uid, a->
gid) < 0)
481 if (lseek (svm_fd, a->
size, SEEK_SET) == (off_t) - 1)
487 if (write (svm_fd, &junk, 1) != 1)
495 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0);
504 memset (rp, 0,
sizeof (*rp));
506 if (pthread_mutexattr_init (&attr))
509 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
512 if (pthread_mutex_init (&rp->
mutex, &attr))
515 if (pthread_mutexattr_destroy (&attr))
518 if (pthread_condattr_init (&cattr))
521 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
524 if (pthread_cond_init (&rp->
condvar, &cattr))
527 if (pthread_condattr_destroy (&cattr))
568 while (overhead_space > 0);
591 return ((
void *) rp);
595 svm_fd = shm_open ((
char *) shm_name, O_RDWR, 0777);
601 perror (
"svm_region_map(mmap open)");
608 if (0 != fstat (svm_fd, &stat))
614 if (stat.st_size > 0)
620 clib_warning (
"waiting for resize of shm file timed out");
625 ts.tv_nsec = 100000000;
626 while (nanosleep (&ts, &tsrem) < 0)
632 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
644 while (rp->
version == 0 && deadman++ < 5)
656 munmap (rp, a->
size);
664 rp = (
void *) mmap ((
void *) a->
baseva, a->
size,
665 PROT_READ | PROT_WRITE,
666 MAP_SHARED | MAP_FIXED, svm_fd, 0);
684 if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0)
687 (
"region %s mutex held by dead pid %d, tag %d, force unlock",
690 rp->
mutex.__data.__owner = 0;
691 rp->
mutex.__data.__lock = 0;
692 dead_region_recovery = 1;
695 if (dead_region_recovery)
702 if (dead_region_recovery)
711 if (dead_region_recovery)
718 return ((
void *) rp);
728 for (i = 0; i <
nheld; i++)
739 uword randomize_baseva;
755 a->
baseva += randomize_baseva;
786 memset (a, 0,
sizeof (*a));
803 memset (a, 0,
sizeof (*a));
820 memset (a, 0,
sizeof (*a));
894 for (i = 0; i < (need_nbits - 1); i++)
907 while (index < root_rp->bitmap_size);
912 clib_warning (
"region %s: not enough VM to allocate 0x%llx (%lld)",
923 clib_warning (
"set %d bits at index %d", need_nbits, index);
926 for (i = 0; i < need_nbits; i++)
960 int i, mypid = getpid ();
963 uword virtual_base, virtual_size;
1006 if (nclients_left == 0)
1008 int index, nbits,
i;
1027 clib_warning (
"clear %d bits at index %d", nbits, index);
1030 for (i = 0; i < nbits; i++)
1059 munmap ((
void *) virtual_base, virtual_size);
1068 munmap ((
void *) virtual_base, virtual_size);
1081 int i, mypid = getpid ();
1082 uword virtual_base, virtual_size;
1122 munmap ((
void *) virtual_base, virtual_size);
1129 int mypid = getpid ();
1165 int mypid = getpid ();
1173 pthread_mutex_lock (&root_rp->
mutex);
1199 name = vec_dup (subp->subregion_name);
1200 vec_add1(svm_names, name);
1204 pthread_mutex_unlock (&root_rp->
mutex);
1206 for (i = 0; i <
vec_len (svm_names); i++)
1210 a->
name = (
char *) svm_names[i];
1214 pthread_mutex_lock (&rp->
mutex);
1218 pthread_mutex_unlock (&rp->
mutex);
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
svm_region_t * svm_get_root_rp(void)
sll srl srl sll sra u16x4 i
static void svm_pop_heap(void *oldheap)
void * svm_map_region(svm_map_region_args_t *a)
Fixed length block allocator.
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
static u64 clib_cpu_time_now(void)
#define hash_set_mem(h, key, value)
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
static u8 * format_svm_size(u8 *s, va_list *args)
static uword clib_bitmap_get_no_check(uword *ai, uword i)
Gets the ith bit value from a bitmap Does not sanity-check the bit position.
u8 * format_mheap(u8 *s, va_list *va)
static uword clib_bitmap_set_no_check(uword *a, uword i, uword new_value)
Sets the ith bit of a bitmap to new_value.
void svm_region_init(void)
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
#define MHEAP_FLAG_DISABLE_VM
void * svm_region_find_or_create(svm_map_region_args_t *a)
static u64 rnd_pagesize(u64 size)
#define clib_warning(format, args...)
#define SVM_FLAGS_NEED_DATA_INIT
#define SVM_GLOBAL_REGION_BASEVA
u8 * format_svm_region(u8 *s, va_list *args)
#define hash_create_string(elts, value_bytes)
#define hash_unset_mem(h, key)
#define pool_put(P, E)
Free an object E in pool P.
#define vec_dup(V)
Return copy of vector (no header, no alignment)
svm_subregion_t * subregions
void svm_region_init_args(svm_map_region_args_t *a)
static void * svm_push_pvt_heap(svm_region_t *rp)
static int root_rp_refcount
static void svm_region_init_internal(svm_map_region_args_t *a)
#define vec_free(V)
Free vector's memory (no header).
#define clib_unix_warning(format, args...)
static pthread_mutex_t * mutexes_held[MAXLOCK]
u8 * shm_name_from_svm_map_region_args(svm_map_region_args_t *a)
void svm_region_init_chroot_uid_gid(char *root_path, int uid, int gid)
static int svm_data_region_map(svm_map_region_args_t *a, svm_region_t *rp)
void * mheap_alloc_with_flags(void *memory, uword memory_size, uword flags)
void svm_region_init_chroot(char *root_path)
#define vec_delete(V, N, M)
Delete N elements starting at element M.
static void region_lock(svm_region_t *rp, int tag)
Bitmaps built as vectors of machine words.
static void region_unlock(svm_region_t *rp)
#define SVM_GLOBAL_REGION_SIZE
#define SVM_GLOBAL_REGION_NAME
#define SVM_PVT_MHEAP_SIZE
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
void svm_region_unmap(void *rp_arg)
void svm_client_scan_this_region_nolock(svm_region_t *rp)
#define hash_get_mem(h, key)
#define CLIB_MEMORY_BARRIER()
static int svm_data_region_create(svm_map_region_args_t *a, svm_region_t *rp)
static u8 * format_svm_flags(u8 *s, va_list *args)
void svm_client_scan(char *root_path)
static void svm_mutex_cleanup(void)
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
static svm_region_t * root_rp