]> git.proxmox.com Git - ceph.git/blob - ceph/src/include/util.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / include / util.h
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) 2012 Inktank Storage, Inc.
7 * Copyright (C) 2014 Red Hat <contact@redhat.com>
8 *
9 * This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License version 2.1, as published by the Free Software
12 * Foundation. See file COPYING.
13 */
14 #ifndef CEPH_UTIL_H
15 #define CEPH_UTIL_H
16
17 #include "common/Formatter.h"
18 #include "include/types.h"
19
20 std::string bytes2str(uint64_t count);
21
22 struct ceph_data_stats
23 {
24 uint64_t byte_total;
25 uint64_t byte_used;
26 uint64_t byte_avail;
27 int avail_percent;
28
29 ceph_data_stats() :
30 byte_total(0),
31 byte_used(0),
32 byte_avail(0),
33 avail_percent(0)
34 { }
35
36 void dump(ceph::Formatter *f) const {
37 ceph_assert(f != NULL);
38 f->dump_int("total", byte_total);
39 f->dump_int("used", byte_used);
40 f->dump_int("avail", byte_avail);
41 f->dump_int("avail_percent", avail_percent);
42 }
43
44 void encode(ceph::buffer::list &bl) const {
45 ENCODE_START(1, 1, bl);
46 encode(byte_total, bl);
47 encode(byte_used, bl);
48 encode(byte_avail, bl);
49 encode(avail_percent, bl);
50 ENCODE_FINISH(bl);
51 }
52
53 void decode(ceph::buffer::list::const_iterator &p) {
54 DECODE_START(1, p);
55 decode(byte_total, p);
56 decode(byte_used, p);
57 decode(byte_avail, p);
58 decode(avail_percent, p);
59 DECODE_FINISH(p);
60 }
61
62 static void generate_test_instances(std::list<ceph_data_stats*>& ls) {
63 ls.push_back(new ceph_data_stats);
64 ls.push_back(new ceph_data_stats);
65 ls.back()->byte_total = 1024*1024;
66 ls.back()->byte_used = 512*1024;
67 ls.back()->byte_avail = 512*1024;
68 ls.back()->avail_percent = 50;
69 }
70 };
71 typedef struct ceph_data_stats ceph_data_stats_t;
72 WRITE_CLASS_ENCODER(ceph_data_stats)
73
74 int get_fs_stats(ceph_data_stats_t &stats, const char *path);
75
76 /// get memory limit for the current cgroup
77 int get_cgroup_memory_limit(uint64_t *limit);
78
79 /// collect info from @p uname(2), @p /proc/meminfo and @p /proc/cpuinfo
80 void collect_sys_info(std::map<std::string, std::string> *m, CephContext *cct);
81
82 #ifdef _WIN32
83 /// Retrieve the actual Windows version, regardless of the app manifest.
84 int get_windows_version(POSVERSIONINFOEXW ver);
85 #endif
86
87 /// dump service ids grouped by their host to the specified formatter
88 /// @param f formatter for the output
89 /// @param services a map from hostname to a list of service id hosted by this host
90 /// @param type the service type of given @p services, for example @p osd or @p mon.
91 void dump_services(ceph::Formatter* f,
92 const std::map<std::string, std::list<int> >& services,
93 const char* type);
94 /// dump service names grouped by their host to the specified formatter
95 /// @param f formatter for the output
96 /// @param services a map from hostname to a list of service name hosted by this host
97 /// @param type the service type of given @p services, for example @p osd or @p mon.
98 void dump_services(ceph::Formatter* f, const std::map<std::string,
99 std::list<std::string> >& services, const char* type);
100
101 std::string cleanbin(ceph::buffer::list &bl, bool &b64, bool show = false);
102 std::string cleanbin(std::string &str);
103
104 namespace ceph::util {
105
106 // Returns true if s matches any parameters:
107 template <typename ...XS>
108 bool match_str(const std::string& s, const XS& ...xs)
109 {
110 return ((s == xs) || ...);
111 }
112
113 } // namespace ceph::util
114 #endif /* CEPH_UTIL_H */