TPIE

2362a60
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 <iostream>
28 #include <iomanip>
29 #include <boost/asio/ip/host_name.hpp>
30 #include <boost/date_time/posix_time/posix_time.hpp>
31 #include <tpie/file.h> // for block size
32 #include <tpie/tpie_log.h>
33 
34 namespace tpie {
35 
38 extern const char * git_commit;
39 
42 extern const char * git_refspec;
43 
47 struct sysinfo {
51  inline sysinfo()
52  : m_platform(calc_platform())
53  , m_hostname(calc_hostname())
54  , m_blocksize(calc_blocksize())
55  {
56  // Does nothing.
57  }
58 
63  inline std::string commit() const { return git_commit; }
64 
69  inline std::string refspec() const { return git_refspec; }
70 
76  inline std::string platform() const { return m_platform; }
77 
81  inline std::string hostname() const { return m_hostname; }
82 
86  inline std::string blocksize() const { return m_blocksize; }
87 
91  inline std::string localtime() const {
92  boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
93  return to_simple_string(now);
94  }
95 
121  template <typename V>
122  inline std::string custominfo(std::string key, const V & value) {
123  std::stringstream builder;
124  if (key != "") key += ':';
125  builder.flags(std::ios::left);
126  builder << std::setw(16) << key << value;
127  return builder.str();
128  }
129 
133  template <typename V>
134  inline void printinfo(std::string key, const V & value) {
135  std::cout << custominfo(key, value) << std::endl;
136  }
137 
138  static inline memory_size_type blocksize_bytes() {
139  return get_block_size();
140  }
141 
142 private:
143  static const char * m_commit;
144  static const char * m_refspec;
145  const std::string m_platform;
146  const std::string m_hostname;
147  const std::string m_blocksize;
148 
149  static inline std::string calc_platform() {
150  std::stringstream p;
151 #ifdef WIN32
152  p << "Windows ";
153 #else
154  p << "Linux ";
155 #endif
156  p << (8*sizeof(size_t)) << "-bit";
157  return p.str();
158  }
159 
160  static inline std::string calc_hostname() {
161  try {
162  return boost::asio::ip::host_name();
163  } catch (boost::system::system_error & e) {
164  log_debug() << "boost::system::system_error thrown while getting hostname. e.what() == " << e.what() << std::endl;
165  return "Exception";
166  }
167  }
168 
169  static inline std::string calc_blocksize() {
170  std::stringstream ss;
171  ss << blocksize_bytes() / 1024
172  << " KiB";
173  return ss.str();
174  }
175 };
176 
181 inline std::ostream & operator<<(std::ostream & s, const sysinfo & info) {
182  return s
183  << "Hostname: " << info.hostname() << '\n'
184  << "Platform: " << info.platform() << '\n'
185  << "Git branch: " << info.refspec() << '\n'
186  << "Git commit: " << info.commit() << '\n'
187  << "Local time: " << info.localtime() << '\n'
188  << "Block size: " << info.blocksize() << '\n'
189  << "Parallel sort: "
190 #ifdef TPIE_PARALLEL_SORT
191  << "Enabled"
192 #else
193  << "Disabled"
194 #endif
195  << '\n'
196  << "Snappy: "
197 #ifdef TPIE_HAS_SNAPPY
198  << "Enabled"
199 #else
200  << "Disabled"
201 #endif
202  << '\n'
203  ;
204 }
205 
206 }
207 
208 #endif // __TPIE_SYSINFO__
memory_size_type get_block_size()
Get the TPIE block size.
std::string refspec() const
Git refspec.
Definition: sysinfo.h:69
void printinfo(std::string key, const V &value)
Print custom info to std::cout.
Definition: sysinfo.h:134
Streams that support substreams.
Class providing system and platform info.
Definition: sysinfo.h:47
Logging functionality and log_level codes for different priorities of log messages.
std::string commit() const
Git commit hash.
Definition: sysinfo.h:63
std::string blocksize() const
Block size used by tpie::ami::stream.
Definition: sysinfo.h:86
std::string custominfo(std::string key, const V &value)
Helper function to make a custom key-value line.
Definition: sysinfo.h:122
std::string hostname() const
System hostname as reported by Boost ASIO.
Definition: sysinfo.h:81
logstream & log_debug()
Return logstream for writing debug log messages.
Definition: tpie_log.h:167
std::string localtime() const
Local date and time in a human-readable format.
Definition: sysinfo.h:91
const char * git_commit
The Git commit hash (40 hexadecimal characters) that TPIE was built from.
const char * git_refspec
The Git refspec that TPIE was built from.
std::string platform() const
Platform description.
Definition: sysinfo.h:76
sysinfo()
Default constructor.
Definition: sysinfo.h:51