static unsigned long alloc_contiguous_memory(u_int *tot_mem_len, u_int *mem_order, int node) { unsigned long mem = 0; if(unlikely(enable_debug)) printk("[DNA] %s(%d)\n", __FUNCTION__, *tot_mem_len); *mem_order = get_order(*tot_mem_len); *tot_mem_len = PAGE_SIZE << *mem_order; if (node >= 0) { mem = __get_free_pages_node(node, GFP_ATOMIC, *mem_order); if (!mem) { printk("[DNA] Warning: memory allocation on node %d failed, using another node", node); node = -1; } } if (!mem) mem = __get_free_pages(GFP_ATOMIC, *mem_order); if(mem) { if(unlikely(enable_debug)) printk("[DNA] %s() success [tot_mem_len=%d,mem=%lu,mem_order=%d,node=%d]\n", __FUNCTION__, *tot_mem_len, mem, *mem_order, node); reserve_memory(mem, *tot_mem_len); } else { if(unlikely(enable_debug)) printk("[DNA] %s() failure (len=%d,order=%d)\n", __FUNCTION__, *tot_mem_len, *mem_order); } return(mem); }
bool os::is_allocatable(size_t bytes) { #ifdef _LP64 return true; #else if (bytes < 2 * G) { return true; } char* addr = reserve_memory(bytes, NULL); if (addr != NULL) { release_memory(addr, bytes); } return addr != NULL; #endif // _LP64 }
typename allocator<T>::pointer allocator<T>::allocate(size_type n, void *const hint) { const size_type nb = n * sizeof(T); # if DEBUG_SMA_TRACE_INTERFACE std::cout << "alloc " << nb << " bytes (n=" << n << ", hint=" << static_cast<void *>(hint) << ")" << std::endl; # endif assert(nb > 0); if (!(nb > 0)) { std::cerr << "cannot allocate a memory block of size " << nb << std::endl; goto badalloc; } if (nb > memfree->size()) { std::cerr << "triing to allocate a memory block of size " << nb << " bytes, " << "but the managed memory has only a size of " << memfree->size() << " bytes." << std::endl; goto badalloc; } {//block auto mask = create_mask(nb, memfree->size()); const size_type pos = find_free_memory(*memfree, mask); assert(0 <= pos && pos <= memfree->size()); if (pos < memfree->size()) { # if DEBUG_SMA_TRACE_MEMALLOCATION std::cout << "reserved memory: " << mask.count() << " bytes -> " << (memfree->count() - mask.count()) << " bytes free." << std::endl; # endif *memfree = reserve_memory(*memfree, std::move(mask)); # if DEBUG_SMA_TRACE_MEMALLOCATION print_free_memory(); # endif return calc_pointer(memstart, pos); } else { # if DEBUG_SMA_TRACE_MEMALLOCATION std::cout << "not enough free memory to allocate " << nb << " bytes." << std::endl; print_free_memory(); # endif } } badalloc: /* not enough memory free */ throw std::bad_alloc(); return nullptr; }
static unsigned long alloc_contiguous_memory(u_int *tot_mem_len, u_int *mem_order) { unsigned long mem; if(unlikely(enable_debug)) printk("[DNA] alloc_contiguous_memory(%d)\n", *tot_mem_len); *mem_order = get_order(*tot_mem_len); *tot_mem_len = PAGE_SIZE << *mem_order; mem = __get_free_pages(GFP_ATOMIC, *mem_order); if(mem) { if(unlikely(enable_debug)) printk("[DNA] alloc_contiguous_memory: success (%d,0x%08lx,%d)\n", *tot_mem_len, mem, *mem_order); reserve_memory(mem, *tot_mem_len); } else { if(unlikely(enable_debug)) printk("[DNA] alloc_contiguous_memory: failure (len=%d,order=%d)\n", *tot_mem_len, *mem_order); } return(mem); }
int main(int argc, char **argv) { int result = 0; int i; userui_ops[0] = &userui_text_ops; userui_ops[1] = FBSPLASH_OPS; userui_ops[2] = USPLASH_OPS; active_ops = &userui_text_ops; handle_params(argc, argv); setup_signal_handlers(); open_console(); open_misc(); if (!test_run) { open_netlink(); get_nofreeze(); get_info(); } lock_memory(); prepare_console(); /* Initialise all that we can, use the first */ // active_ops = NULL; for (i = 0; i < NUM_UIS; i++) { if (userui_ops[i] && userui_ops[i]->load) { result = userui_ops[i]->load(); if (result) { if (test_run) fprintf(stderr, "Failed to initialise %s module.\n", userui_ops[i]->name); else printk("Failed to initialise %s module.\n", userui_ops[i]->name); } else if (!active_ops) active_ops = userui_ops[i]; } } if (active_ops->prepare) active_ops->prepare(); register_keypress_handler(); need_cleanup = 1; running = 1; result = nice(1); if (active_ops->memory_required) reserve_memory(active_ops->memory_required()); else reserve_memory(4*1024*1024); /* say 4MB */ enforce_lifesavers(); if (test_run) { safe_to_exit = 0; do_test_run(); return 0; } if (send_ready()) message_loop(); /* The only point we ever reach here is if message_loop crashed out. * If this is the case, we should spin for a few hours before exiting to * ensure that we don't corrupt stuff on disk (if we're past the atomic * copy). */ sleep(60*60*1); /* 1 hours */ _exit(1); }
int master_init(int argc, char ** argv) { static int cpu_list_is_set = 0; static int cpu_str_is_set = 0; static int mem_str_is_set = 0; int numa_zone = -1; int num_cpus = -1; uint64_t mem_size_MB = -1; hdb_db_t db = NULL; char * cpu_list = NULL; /* Create the master database */ db = create_master_db(); if (db == NULL) { printf("Error creating master database\n"); return -1; } /* Begin the resource reservations */ { char c = 0; int opt_index = 0; static struct option long_options[] = { {"cpu", required_argument, NULL, 'c'}, {"numa", required_argument, NULL, 'n'}, {"cpulist", required_argument, &cpu_list_is_set, 1 }, {"mem", required_argument, NULL, 'm'}, {0, 0, 0, 0} }; while ((c = getopt_long_only(argc, argv, "c:n:m:", long_options, &opt_index)) != -1) { switch (c) { case 'c': num_cpus = smart_atoi(-1, optarg); if (num_cpus == -1) { ERROR("Invalid CPU argument (%s)\n", optarg); usage(argv[0]); } cpu_str_is_set = 1; break; case 'n': numa_zone = smart_atoi(-1, optarg); if (numa_zone == -1) { ERROR("Invalid NUMA argument (%s)\n", optarg); usage(argv[0]); } break; case 'm': mem_size_MB = smart_atou64(-1, optarg); if (mem_size_MB == (uint64_t)-1) { ERROR("Invalid Memory argument (%s)\n", optarg); usage(argv[0]); } mem_str_is_set = 1; break; case 0: { switch (opt_index) { case 2: cpu_list = optarg; break; } break; } case '?': default: usage(argv[0]); break; } } } if ((cpu_list_is_set != 0) && (cpu_str_is_set != 0)) { fprintf(stderr, "Error: Cannot use both --cpu and --cpulist options at the same time\n"); exit(-1); } if (cpu_list_is_set) { if (reserve_cpu_list(cpu_list) == -1) { ERROR("Could not reserve CPU list (%s)\n", cpu_list); return -1; } } else if (cpu_str_is_set) { if (reserve_cpus(num_cpus, numa_zone) == -1) { ERROR("Could not reserve %d CPUs on NUMA zone %d\n", num_cpus, numa_zone); return -1; } } else { /* Default to reserve CPU 0 */ if (reserve_cpu_list("0") == -1) { ERROR("Could not reserve CPU 0 (default) for master enclave\n"); return -1; } } if (mem_str_is_set) { if (reserve_memory(mem_size_MB, numa_zone) == -1) { ERROR("Could not reserve %luMB of memory on NUMA zone %d for master enclave\n", mem_size_MB, numa_zone); return -1; } } else { /* Default to reserve 1GB on NUMA node 0 */ if (reserve_memory(1024, 0) == -1) { ERROR("Could not reserve default 1GB of memory on NUMA zone 0\n"); return -1; } } /* * Configure allocation policies to avoid non-reserved CPUs */ { struct pet_cpu * cpu_arr = NULL; uint32_t cpu_cnt = 0; uint32_t i = 0; int ret = 0; ret = pet_probe_cpus(&cpu_cnt, &cpu_arr); if (ret != 0) { fprintf(stderr, "Error: Could not probe CPUs\n"); exit(-1); } for (i = 0; i < cpu_cnt; i++) { if (cpu_arr[i].state == PET_CPU_OFFLINE) { fprintf(stderr, "Error: Encountered Offline CPU [%d].\n", cpu_arr[i].cpu_id); fprintf(stderr, "\tSystem maybe in an inconsistent state\n"); continue; } if (cpu_arr[i].state == PET_CPU_INVALID) { fprintf(stderr, "Error: Encountered INVALID CPU [%d].\n", cpu_arr[i].cpu_id); fprintf(stderr, "\tSystem management maybe BUGGY\n"); continue; } if (v3_is_vmm_present()) { if (cpu_arr[i].state != PET_CPU_RSVD) { printf("Removing CPU %d from Palacios/Linux\n", cpu_arr[i].cpu_id); v3_remove_cpu(cpu_arr[i].cpu_id); } } } } /* Initialize the system resource state */ populate_system_info(db); /* Export Database */ if (export_master_db() == -1) { ERROR("Could not export Master Database\n"); return -1; } // wg_print_db(db); return 0; }