TPIE

11a2c2d
sysinfo.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2012, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
23 
24 #ifndef __TPIE_SYSINFO__
25 #define __TPIE_SYSINFO__
26 
27 #include <tpie/tpie_export.h>
28 #include <iostream>
29 #include <iomanip>
30 #include <boost/asio/ip/host_name.hpp>
31 #include <boost/date_time/posix_time/posix_time.hpp>
32 #include <tpie/file.h> // for block size
33 #include <tpie/tpie_log.h>
34 
35 namespace tpie {
36 
39 TPIE_EXPORT extern const char * git_commit;
40 
43 TPIE_EXPORT extern const char * git_refspec;
44 
48 struct sysinfo {
52  inline sysinfo()
53  : m_platform(calc_platform())
54  , m_hostname(calc_hostname())
55  , m_blocksize(calc_blocksize())
56  {
57  // Does nothing.
58  }
59 
64  inline std::string commit() const { return git_commit; }
65 
70  inline std::string refspec() const { return git_refspec; }
71 
77  inline std::string platform() const { return m_platform; }
78 
82  inline std::string hostname() const { return m_hostname; }
83 
87  inline std::string blocksize() const { return m_blocksize; }
88 
92  inline std::string localtime() const {
93  boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
94  return to_simple_string(now);
95  }
96 
122  template <typename V>
123  inline std::string custominfo(std::string key, const V & value) {
124  std::stringstream builder;
125  if (key != "") key += ':';
126  builder.flags(std::ios::left);
127  builder << std::setw(16) << key << value;
128  return builder.str();
129  }
130 
134  template <typename V>
135  inline void printinfo(std::string key, const V & value) {
136  std::cout << custominfo(key, value) << std::endl;
137  }
138 
139  static inline memory_size_type blocksize_bytes() {
140  return get_block_size();
141  }
142 
143 private:
144  static const char * m_commit;
145  static const char * m_refspec;
146  const std::string m_platform;
147  const std::string m_hostname;
148  const std::string m_blocksize;
149 
150  static inline std::string calc_platform() {
151  std::stringstream p;
152 #ifdef WIN32
153  p << "Windows ";
154 #else
155  p << "Linux ";
156 #endif
157  p << (8*sizeof(size_t)) << "-bit";
158  return p.str();
159  }
160 
161  static inline std::string calc_hostname() {
162  try {
163  return boost::asio::ip::host_name();
164  } catch (boost::system::system_error & e) {
165  log_debug() << "boost::system::system_error thrown while getting hostname. e.what() == " << e.what() << std::endl;
166  return "Exception";
167  }
168  }
169 
170  static inline std::string calc_blocksize() {
171  std::stringstream ss;
172  ss << blocksize_bytes() / 1024
173  << " KiB";
174  return ss.str();
175  }
176 };
177 
182 inline std::ostream & operator<<(std::ostream & s, const sysinfo & info) {
183  return s
184  << "Hostname: " << info.hostname() << '\n'
185  << "Platform: " << info.platform() << '\n'
186  << "Git branch: " << info.refspec() << '\n'
187  << "Git commit: " << info.commit() << '\n'
188  << "Local time: " << info.localtime() << '\n'
189  << "Block size: " << info.blocksize() << '\n'
190  << "Parallel sort: "
191 #ifdef TPIE_PARALLEL_SORT
192  << "Enabled"
193 #else
194  << "Disabled"
195 #endif
196  << '\n'
197  << "Snappy: "
198 #ifdef TPIE_HAS_SNAPPY
199  << "Enabled"
200 #else
201  << "Disabled"
202 #endif
203  << '\n'
204  ;
205 }
206 
207 }
208 
209 #endif // __TPIE_SYSINFO__
tpie::sysinfo::localtime
std::string localtime() const
Local date and time in a human-readable format.
Definition: sysinfo.h:92
tpie::sysinfo::platform
std::string platform() const
Platform description.
Definition: sysinfo.h:77
tpie::git_refspec
const TPIE_EXPORT char * git_refspec
The Git refspec that TPIE was built from.
tpie::sysinfo::commit
std::string commit() const
Git commit hash.
Definition: sysinfo.h:64
tpie::sysinfo::sysinfo
sysinfo()
Default constructor.
Definition: sysinfo.h:52
tpie::git_commit
const TPIE_EXPORT char * git_commit
The Git commit hash (40 hexadecimal characters) that TPIE was built from.
tpie::sysinfo
Class providing system and platform info.
Definition: sysinfo.h:48
tpie::sysinfo::hostname
std::string hostname() const
System hostname as reported by Boost ASIO.
Definition: sysinfo.h:82
tpie::log_debug
logstream & log_debug()
Return logstream for writing debug log messages.
Definition: tpie_log.h:168
tpie::sysinfo::printinfo
void printinfo(std::string key, const V &value)
Print custom info to std::cout.
Definition: sysinfo.h:135
tpie::get_block_size
TPIE_EXPORT memory_size_type get_block_size()
Get the TPIE block size.
tpie_log.h
tpie::sysinfo::refspec
std::string refspec() const
Git refspec.
Definition: sysinfo.h:70
file.h
tpie
Definition: access_type.h:26
tpie::sysinfo::custominfo
std::string custominfo(std::string key, const V &value)
Helper function to make a custom key-value line.
Definition: sysinfo.h:123
tpie::sysinfo::blocksize
std::string blocksize() const
Block size used by tpie::ami::stream.
Definition: sysinfo.h:87