Created
August 28, 2019 18:58
-
-
Save thesues/8546f56653dce07845eb384601a4a0db to your computer and use it in GitHub Desktop.
test cephfs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define __USE_FILE_OFFSET64 | |
#include <cephfs/libcephfs.h> | |
#include <errno.h> | |
#include <fcntl.h> | |
#include <string> | |
#include <iostream> | |
#include <time.h> | |
#include <pthread.h> | |
#include <vector> | |
void timespec_diff(struct timespec *start, struct timespec *stop, | |
struct timespec *result) | |
{ | |
if ((stop->tv_nsec - start->tv_nsec) < 0) { | |
result->tv_sec = stop->tv_sec - start->tv_sec - 1; | |
result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000; | |
} else { | |
result->tv_sec = stop->tv_sec - start->tv_sec; | |
result->tv_nsec = stop->tv_nsec - start->tv_nsec; | |
} | |
return; | |
} | |
struct WorkerArgs { | |
int start_num; | |
int end_num; | |
// struct ceph_mount_info * cmount; | |
}; | |
void *worker(void *main_args) { | |
char name_buf[100]; | |
char buf[10]; | |
int ret; | |
struct timespec ts, ts_start, ts_end; | |
struct WorkerArgs * args = (struct WorkerArgs*) main_args; | |
int i = args->start_num; | |
int end_num = args->end_num; | |
//TODO | |
struct ceph_mount_info *cmount; | |
if ((ret = ceph_create(&cmount, NULL)) != 0) { | |
perror("ceph_create failed"); | |
return 0; | |
} | |
if ((ret = ceph_conf_read_file(cmount, NULL)) != 0 ) { | |
perror("ceph_conf_read failed"); | |
return 0; | |
} | |
if ((ret = ceph_mount(cmount, NULL)) != 0) { | |
perror("ceph_mount failed"); | |
return 0; | |
} | |
if ((ret = ceph_chdir(cmount, "/fs500")) != 0) { | |
perror("ceph_chdir failed"); | |
return 0; | |
} | |
//TODO START counting | |
int id = pthread_self(); | |
clock_gettime(CLOCK_MONOTONIC, &ts_start); | |
for (;i< end_num; i++) { | |
sprintf(name_buf, "%x%s%d", id, "fuck", i); | |
//struct statvfs stat_buf; | |
//ceph_statfs(cmount, ".", &stat_buf); | |
int fd = ceph_open(cmount, name_buf, O_CREAT|O_RDWR, 0666); | |
if (fd < 0) { | |
perror("ceph_write failed"); | |
return 0; | |
} | |
if (ceph_write(cmount, fd, "testdata", 8, 0) < 0) { | |
perror("ceph_write failed"); | |
return 0; | |
} | |
ceph_close(cmount, fd); | |
fd = ceph_open(cmount, name_buf, O_RDONLY, 0); | |
if (fd < 0) { | |
perror("ceph_open failed"); | |
return 0; | |
} | |
ceph_read(cmount, fd, buf, 0, 8); | |
std::cout << buf; | |
ceph_close(cmount, fd); | |
} | |
//TODO END counting | |
clock_gettime(CLOCK_MONOTONIC, &ts_end); | |
timespec_diff(&ts_start, &ts_end, &ts); | |
std::cout << "Time escalapsed :" << ts.tv_sec << "s " << ts.tv_nsec / 1000000 << "ms" << std::endl; | |
ceph_unmount(cmount); | |
ceph_release(cmount); | |
} | |
int main(int argc, char**argv) { | |
int workers; | |
if (argc != 2) { | |
perror("Usage: ./a.out <worker threads>"); | |
return -1; | |
}else { | |
workers = atoi(argv[1]); | |
} | |
std::vector<pthread_t> worker_threads(workers); | |
struct WorkerArgs * args = new WorkerArgs[workers]; | |
int size = 500 / workers; | |
int start_num = 0; | |
int end_num = size; | |
for(int i = 0 ; i < workers ; i ++) { | |
args[i].start_num = start_num; | |
args[i].end_num = end_num; | |
pthread_create(&worker_threads[i], NULL, worker, (void*)&args[i]); | |
start_num += size; | |
end_num += size; | |
} | |
for (auto && t : worker_threads) { | |
pthread_join(t, NULL); | |
} | |
delete []args; | |
/* | |
for(i = 0 ; i < 500; i ++) { | |
std::string name = prefix + std::to_string(i); | |
int fd = ceph_open(cmount, name.c_str(), O_CREAT|O_RDWR|O_TRUNC, 0666); | |
if (ceph_write(cmount, fd, "testdata", 8, 0) < 0) { | |
perror("ceph_write failed"); | |
} | |
ceph_close(cmount, fd); | |
fd = ceph_open(cmount, name.c_str(), O_RDONLY, 0); | |
if (fd < 0) { | |
perror("ceph_open failed"); | |
} | |
std::cout << buf; | |
ceph_read(cmount, fd, buf, 0, 8); | |
ceph_close(cmount, fd); | |
} | |
*/ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment