bool stat_manager::start(int bucket_number, const char *file_dir) { // open file snprintf(file_name, PATH_MAX_LENGTH, "%s/tair_kdb_%06d.stat", file_dir, bucket_number); fd =::open(file_name, O_RDWR | O_CREAT | O_LARGEFILE, 0600); if(fd < 0) { log_error("open file [%s] failed: %s", file_name, strerror(errno)); return false; } // check the file is new or not if(get_size() == 0) { if(!initial_stat_file()) { return false; } } // mmap int flags = PROT_READ | PROT_WRITE; int size = KDBSTATINFO_SIZE; if(!ensure_file_size(size)) { log_error("ensure file size failed"); return false; } void* data = mmap(0, size, flags, MAP_SHARED, fd, 0); if(data == MAP_FAILED) { log_error("map file failed: %s", strerror(errno)); return false; } log_info("mmap file successed, maped size is: [%d]", size); stat_info = (kdb_stat_info *)data; return true; }
DLL_VARIABLE void mj_rollingfile_write(mj_rollingfile_t* rf, const char* data, size_t n) { if(0 == rf) return; ensure_file_size(rf); if(0 == rf->out) return; fwrite(data, sizeof(char), n, rf->out); rf->current_size += n; }
int WOShmem_init(const char *file, size_t memsize) { int error = 0; char *errMsg = NULL; /* open up the file */ WOShmem_fd = open(file, O_RDWR|O_CREAT, 0600); if (WOShmem_fd > 0) { #ifdef APACHE /* unlink the file so when Apache exits the file is removed */ /* The mmap should still be in effect */ unlink(file); #endif WOShmem_size = ensure_file_size(WOShmem_fd, memsize); if (WOShmem_size != -1) { WOShmem_base_address = (void *)mmap(0, WOShmem_size, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED|MAP_INHERIT, WOShmem_fd, 0); if (WOShmem_base_address == MAP_FAILED) { errMsg = WA_errorDescription(WA_error()); WOLog(WO_ERR, "WOShmem_init(): couldn't map file: %s", errMsg); error = 1; } } else error = 1; } else { errMsg = WA_errorDescription(WA_error()); WOLog(WO_ERR,"WOShmem_init(): Couldn't open %s: %s", file, errMsg); error = 1; } if (error) WOShmem_fd = -1; if (errMsg) WA_freeErrorDescription(errMsg); WOShmem_mutex = WA_createLock("WOShmem_lock"); return WOShmem_fd == -1; }
bool map_file(bool write = false) { int flags = PROT_READ; if(write) flags |= PROT_WRITE; if(fd < 0) return false; if(0 == max_size) return false; if(max_size <= (1024 * MB_SIZE)) { size = max_size; } else { size = 1 * MB_SIZE; } if(!ensure_file_size(size)) { log_error("ensure file size failed"); return false; } data = mmap(0, size, flags, MAP_SHARED, fd, 0); if(data == MAP_FAILED) { log_error("map file failed: %s", strerror(errno)); fd = -1; data = NULL; size = 0; return false; } log_info("mmap file successed, maped size is: [%d]", size); return true; }
bool remap() { if(fd < 0 || data == NULL) { log_error("mremap not mapped yet"); return false; } if(size == max_size) { log_info("already mapped max size, currSize: [%u], maxSize: [%u]", size, max_size); return false; } size_t new_size = size * 2; if(new_size > max_size) new_size = max_size; if(!ensure_file_size(new_size)) { log_error("ensure file size failed in mremap"); return false; } //void *newMapData = mremap(m_data, m_size, newSize, MREMAP_MAYMOVE); void *new_map_data = mremap(data, size, new_size, 0); if(new_map_data == MAP_FAILED) { log_error("mremap file failed: %s", strerror(errno)); return false; } else { log_info("remap success, oldSize: [%u], newSize: [%u]", size, new_size); } log_info("mremap successed, new size: [%d]", new_size); data = new_map_data; size = new_size; return true; }