Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / bench / dumb_backend.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2
3 #include "acconfig.h"
4
5 #include <unistd.h>
6 #include "dumb_backend.h"
7
8 string DumbBackend::get_full_path(const string &oid)
9 {
10         return path + "/" + oid;
11 }
12
13 void DumbBackend::_write(
14   const string &oid,
15   uint64_t offset,
16   const bufferlist &bl,
17   Context *on_applied,
18   Context *on_commit)
19 {
20   string full_path(get_full_path(oid));
21   int fd = ::open(
22     full_path.c_str(), O_CREAT|O_WRONLY, 0777);
23   if (fd < 0) {
24     std::cout << full_path << ": errno is " << errno << std::endl;
25     ceph_abort();
26   }
27
28   int r =  ::lseek(fd, offset, SEEK_SET);
29   if (r < 0) {
30     r = errno;
31     std::cout << "lseek failed, errno is: " << r << std::endl;
32     ::close(fd);
33     return;
34   }
35   bl.write_fd(fd);
36   on_applied->complete(0);
37   if (do_fsync)
38     ::fsync(fd);
39 #ifdef HAVE_SYNC_FILE_RANGE
40   if (do_sync_file_range)
41     ::sync_file_range(fd, offset, bl.length(),
42                       SYNC_FILE_RANGE_WAIT_AFTER);
43 #else
44 # warning "sync_file_range not supported!"
45 #endif
46 #ifdef HAVE_POSIX_FADVISE
47   if (do_fadvise) {
48     int fa_r = ::posix_fadvise(fd, offset, bl.length(), POSIX_FADV_DONTNEED);
49     if (fa_r) {
50         std::cout << "posix_fadvise failed, errno is: " << fa_r << std::endl;
51     }
52   }
53 #else
54 # warning "posix_fadvise not supported!"
55 #endif
56   ::close(fd);
57   {
58     Mutex::Locker l(pending_commit_mutex);
59     pending_commits.insert(on_commit);
60   }
61   sem.Put();
62 }
63
64 void DumbBackend::read(
65   const string &oid,
66   uint64_t offset,
67   uint64_t length,
68   bufferlist *bl,
69   Context *on_complete)
70 {
71   string full_path(get_full_path(oid));
72   int fd = ::open(
73     full_path.c_str(), 0, O_RDONLY);
74   if (fd < 0) return;
75
76   int r = ::lseek(fd, offset, SEEK_SET);
77   if (r < 0) {
78     r = errno;
79     std::cout << "lseek failed, errno is: " << r << std::endl;
80     ::close(fd);
81     return;
82   }
83
84   bl->read_fd(fd, length);
85   ::close(fd);
86   on_complete->complete(0);
87 }
88
89 void DumbBackend::sync_loop()
90 {
91   while (1) {
92     sleep(sync_interval);
93     {
94       Mutex::Locker l(sync_loop_mutex);
95       if (sync_loop_stop != 0) {
96         sync_loop_stop = 2;
97         sync_loop_cond.Signal();
98         break;
99       }
100     }
101     tp.pause();
102 #ifdef HAVE_SYS_SYNCFS
103     ::syncfs(sync_fd);
104 #else
105     ::sync();
106 #endif
107     {
108       Mutex::Locker l(pending_commit_mutex);
109       for (set<Context*>::iterator i = pending_commits.begin();
110            i != pending_commits.end();
111            pending_commits.erase(i++)) {
112         (*i)->complete(0);
113       }
114     }
115     tp.unpause();
116   }
117 }