]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/mon/PGMap.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / test / mon / PGMap.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) 2014 Inktank <info@inktank.com>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License version 2, as published by the Free Software
11 * Foundation. See file COPYING.
12 */
13
14 #include "mon/PGMap.h"
15 #include "gtest/gtest.h"
16
17 #include "include/stringify.h"
18
19
20 namespace {
21 class CheckTextTable : public TextTable {
22 public:
23 explicit CheckTextTable(bool verbose) {
24 for (int i = 0; i < 5; i++) {
25 define_column("", TextTable::LEFT, TextTable::LEFT);
26 }
27 if (verbose) {
28 for (int i = 0; i < 5; i++) {
29 define_column("", TextTable::LEFT, TextTable::LEFT);
30 }
31 }
32 }
33 const string& get(unsigned r, unsigned c) const {
34 ceph_assert(r < row.size());
35 ceph_assert(c < row[r].size());
36 return row[r][c];
37 }
38 };
39
40 // copied from PGMap.cc
41 string percentify(float a) {
42 stringstream ss;
43 if (a < 0.01)
44 ss << "0";
45 else
46 ss << std::fixed << std::setprecision(2) << a;
47 return ss.str();
48 }
49 }
50
51 // dump_object_stat_sum() is called by "ceph df" command
52 // with table, without formatter, verbose = true, not empty, avail > 0
53 TEST(pgmap, dump_object_stat_sum_0)
54 {
55 bool verbose = true;
56 CheckTextTable tbl(verbose);
57 pool_stat_t pool_stat;
58 object_stat_sum_t& sum = pool_stat.stats.sum;
59 sum.num_bytes = 42 * 1024 * 1024;
60 sum.num_objects = 42;
61 sum.num_objects_degraded = 13; // there are 13 missings + not_yet_backfilled
62 sum.num_objects_dirty = 2;
63 sum.num_rd = 100;
64 sum.num_rd_kb = 123;
65 sum.num_wr = 101;
66 sum.num_wr_kb = 321;
67 pool_stat.num_store_stats = 3;
68 store_statfs_t &statfs = pool_stat.store_stats;
69 statfs.data_stored = 40 * 1024 * 1024;
70 statfs.allocated = 41 * 1024 * 1024 * 2;
71 statfs.data_compressed_allocated = 4334;
72 statfs.data_compressed_original = 1213;
73
74 sum.calc_copies(3); // assuming we have 3 copies for each obj
75 // nominal amount of space available for new objects in this pool
76 uint64_t avail = 2016 * 1024 * 1024;
77 pg_pool_t pool;
78 pool.quota_max_objects = 2000;
79 pool.quota_max_bytes = 2000 * 1024 * 1024;
80 pool.size = 2;
81 pool.type = pg_pool_t::TYPE_REPLICATED;
82 PGMap::dump_object_stat_sum(tbl, nullptr, pool_stat, avail,
83 pool.get_size(), verbose, &pool);
84 float copies_rate =
85 (static_cast<float>(sum.num_object_copies - sum.num_objects_degraded) /
86 sum.num_object_copies) * pool.get_size();
87 float used_percent = (float)statfs.allocated /
88 (statfs.allocated + avail) * 100;
89 uint64_t stored = statfs.data_stored / copies_rate;
90
91 unsigned col = 0;
92 ASSERT_EQ(stringify(byte_u_t(stored)), tbl.get(0, col++));
93 ASSERT_EQ(stringify(si_u_t(sum.num_objects)), tbl.get(0, col++));
94 ASSERT_EQ(stringify(byte_u_t(statfs.allocated)), tbl.get(0, col++));
95 ASSERT_EQ(percentify(used_percent), tbl.get(0, col++));
96 ASSERT_EQ(stringify(byte_u_t(avail/copies_rate)), tbl.get(0, col++));
97 ASSERT_EQ(stringify(si_u_t(pool.quota_max_objects)), tbl.get(0, col++));
98 ASSERT_EQ(stringify(byte_u_t(pool.quota_max_bytes)), tbl.get(0, col++));
99 ASSERT_EQ(stringify(si_u_t(sum.num_objects_dirty)), tbl.get(0, col++));
100 ASSERT_EQ(stringify(byte_u_t(statfs.data_compressed_allocated)), tbl.get(0, col++));
101 ASSERT_EQ(stringify(byte_u_t(statfs.data_compressed_original)), tbl.get(0, col++));
102 }
103
104 // with table, without formatter, verbose = true, empty, avail > 0
105 TEST(pgmap, dump_object_stat_sum_1)
106 {
107 bool verbose = true;
108 CheckTextTable tbl(verbose);
109 pool_stat_t pool_stat;
110 object_stat_sum_t& sum = pool_stat.stats.sum; // zero by default
111 ASSERT_TRUE(sum.is_zero());
112 // nominal amount of space available for new objects in this pool
113 uint64_t avail = 2016 * 1024 * 1024;
114 pg_pool_t pool;
115 pool.quota_max_objects = 2000;
116 pool.quota_max_bytes = 2000 * 1024 * 1024;
117 pool.size = 2;
118 pool.type = pg_pool_t::TYPE_REPLICATED;
119 PGMap::dump_object_stat_sum(tbl, nullptr, pool_stat, avail,
120 pool.get_size(), verbose, &pool);
121 unsigned col = 0;
122 ASSERT_EQ(stringify(byte_u_t(0)), tbl.get(0, col++));
123 ASSERT_EQ(stringify(si_u_t(0)), tbl.get(0, col++));
124 ASSERT_EQ(stringify(byte_u_t(0)), tbl.get(0, col++));
125 ASSERT_EQ(percentify(0), tbl.get(0, col++));
126 ASSERT_EQ(stringify(byte_u_t(avail/pool.size)), tbl.get(0, col++));
127 ASSERT_EQ(stringify(si_u_t(pool.quota_max_objects)), tbl.get(0, col++));
128 ASSERT_EQ(stringify(byte_u_t(pool.quota_max_bytes)), tbl.get(0, col++));
129 ASSERT_EQ(stringify(si_u_t(0)), tbl.get(0, col++));
130 ASSERT_EQ(stringify(byte_u_t(0)), tbl.get(0, col++));
131 ASSERT_EQ(stringify(byte_u_t(0)), tbl.get(0, col++));
132 }
133
134 // with table, without formatter, verbose = false, empty, avail = 0
135 TEST(pgmap, dump_object_stat_sum_2)
136 {
137 bool verbose = false;
138 CheckTextTable tbl(verbose);
139 pool_stat_t pool_stat;
140 object_stat_sum_t& sum = pool_stat.stats.sum; // zero by default
141 ASSERT_TRUE(sum.is_zero());
142 // nominal amount of space available for new objects in this pool
143 uint64_t avail = 0;
144 pg_pool_t pool;
145 pool.quota_max_objects = 2000;
146 pool.quota_max_bytes = 2000 * 1024 * 1024;
147 pool.size = 2;
148 pool.type = pg_pool_t::TYPE_REPLICATED;
149
150 PGMap::dump_object_stat_sum(tbl, nullptr, pool_stat, avail,
151 pool.get_size(), verbose, &pool);
152 unsigned col = 0;
153 ASSERT_EQ(stringify(byte_u_t(0)), tbl.get(0, col++));
154 ASSERT_EQ(stringify(si_u_t(0)), tbl.get(0, col++));
155 ASSERT_EQ(stringify(byte_u_t(0)), tbl.get(0, col++));
156 ASSERT_EQ(percentify(0), tbl.get(0, col++));
157 ASSERT_EQ(stringify(byte_u_t(avail/pool.size)), tbl.get(0, col++));
158 }