]> git.proxmox.com Git - ceph.git/blob - ceph/src/os/bluestore/BlockDevice.cc
import 15.2.5
[ceph.git] / ceph / src / os / bluestore / BlockDevice.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2015 XSky <haomai@xsky.com>
7 *
8 * Author: Haomai Wang <haomaiwang@gmail.com>
9 *
10 * This is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License version 2.1, as published by the Free Software
13 * Foundation. See file COPYING.
14 *
15 */
16
17 #include <libgen.h>
18 #include <unistd.h>
19
20 #include "BlockDevice.h"
21
22 #if defined(HAVE_LIBAIO) || defined(HAVE_POSIXAIO)
23 #include "KernelDevice.h"
24 #endif
25
26 #if defined(HAVE_SPDK)
27 #include "NVMEDevice.h"
28 #endif
29
30 #if defined(HAVE_BLUESTORE_PMEM)
31 #include "PMEMDevice.h"
32 #include "libpmem.h"
33 #endif
34
35 #include "common/debug.h"
36 #include "common/EventTrace.h"
37 #include "common/errno.h"
38 #include "include/compat.h"
39
40 #define dout_context cct
41 #define dout_subsys ceph_subsys_bdev
42 #undef dout_prefix
43 #define dout_prefix *_dout << "bdev "
44
45 void IOContext::aio_wait()
46 {
47 std::unique_lock l(lock);
48 // see _aio_thread for waker logic
49 while (num_running.load() > 0) {
50 dout(10) << __func__ << " " << this
51 << " waiting for " << num_running.load() << " aios to complete"
52 << dendl;
53 cond.wait(l);
54 }
55 dout(20) << __func__ << " " << this << " done" << dendl;
56 }
57
58 uint64_t IOContext::get_num_ios() const
59 {
60 // this is about the simplest model for transaction cost you can
61 // imagine. there is some fixed overhead cost by saying there is a
62 // minimum of one "io". and then we have some cost per "io" that is
63 // a configurable (with different hdd and ssd defaults), and add
64 // that to the bytes value.
65 uint64_t ios = 0;
66 #if defined(HAVE_LIBAIO) || defined(HAVE_POSIXAIO)
67 ios += pending_aios.size();
68 #endif
69 #ifdef HAVE_SPDK
70 ios += total_nseg;
71 #endif
72 return ios;
73 }
74
75 void IOContext::release_running_aios()
76 {
77 ceph_assert(!num_running);
78 #if defined(HAVE_LIBAIO) || defined(HAVE_POSIXAIO)
79 // release aio contexts (including pinned buffers).
80 running_aios.clear();
81 #endif
82 }
83
84 BlockDevice *BlockDevice::create(CephContext* cct, const string& path,
85 aio_callback_t cb, void *cbpriv, aio_callback_t d_cb, void *d_cbpriv)
86 {
87 string type = "kernel";
88 char buf[PATH_MAX + 1];
89 int r = ::readlink(path.c_str(), buf, sizeof(buf) - 1);
90 if (r >= 0) {
91 buf[r] = '\0';
92 char *bname = ::basename(buf);
93 if (strncmp(bname, SPDK_PREFIX, sizeof(SPDK_PREFIX)-1) == 0)
94 type = "ust-nvme";
95 }
96
97 #if defined(HAVE_BLUESTORE_PMEM)
98 if (type == "kernel") {
99 int is_pmem = 0;
100 size_t map_len = 0;
101 void *addr = pmem_map_file(path.c_str(), 0, PMEM_FILE_EXCL, O_RDONLY, &map_len, &is_pmem);
102 if (addr != NULL) {
103 if (is_pmem)
104 type = "pmem";
105 else
106 dout(1) << path.c_str() << " isn't pmem file" << dendl;
107 pmem_unmap(addr, map_len);
108 } else {
109 dout(1) << "pmem_map_file:" << path.c_str() << " failed." << pmem_errormsg() << dendl;
110 }
111 }
112 #endif
113
114 dout(1) << __func__ << " path " << path << " type " << type << dendl;
115
116 #if defined(HAVE_BLUESTORE_PMEM)
117 if (type == "pmem") {
118 return new PMEMDevice(cct, cb, cbpriv);
119 }
120 #endif
121 #if defined(HAVE_LIBAIO) || defined(HAVE_POSIXAIO)
122 if (type == "kernel") {
123 return new KernelDevice(cct, cb, cbpriv, d_cb, d_cbpriv);
124 }
125 #endif
126 #ifndef WITH_SEASTAR
127 #if defined(HAVE_SPDK)
128 if (type == "ust-nvme") {
129 return new NVMEDevice(cct, cb, cbpriv);
130 }
131 #endif
132 #endif
133
134 derr << __func__ << " unknown backend " << type << dendl;
135 ceph_abort();
136 return NULL;
137 }
138
139 void BlockDevice::queue_reap_ioc(IOContext *ioc)
140 {
141 std::lock_guard l(ioc_reap_lock);
142 if (ioc_reap_count.load() == 0)
143 ++ioc_reap_count;
144 ioc_reap_queue.push_back(ioc);
145 }
146
147 void BlockDevice::reap_ioc()
148 {
149 if (ioc_reap_count.load()) {
150 std::lock_guard l(ioc_reap_lock);
151 for (auto p : ioc_reap_queue) {
152 dout(20) << __func__ << " reap ioc " << p << dendl;
153 delete p;
154 }
155 ioc_reap_queue.clear();
156 --ioc_reap_count;
157 }
158 }