0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
System.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 #include <Common/Compat.h>
27 #include <Common/FileUtils.h>
28 #include <Common/Logger.h>
29 #include <Common/Path.h>
30 #include <Common/SystemInfo.h>
31 
32 #include <boost/algorithm/string.hpp>
33 #include <boost/filesystem.hpp>
34 
35 #include <iostream>
36 #include <vector>
37 
38 using namespace Hypertable;
39 using namespace std;
40 using namespace boost::filesystem;
41 
42 string System::install_dir;
43 std::unique_ptr<ClusterDefinition::ClusterDefinition> System::cluster_def;
44 string System::exe_name;
46 string System::tm_zone;
47 bool System::ms_initialized = false;
49 
50 String System::locate_install_dir(const char *argv0) {
51  lock_guard<mutex> lock(ms_mutex);
52  return _locate_install_dir(argv0);
53 }
54 
55 String System::_locate_install_dir(const char *argv0) {
56 
57  if (!install_dir.empty())
58  return install_dir;
59 
60  exe_name = Path(argv0).filename().generic_string();
61 
62  Path exepath(proc_info().exe);
63 
64  // Detect install_dir/bin/exe_name: assumed install layout
65  if (exepath.parent_path().filename() == "bin")
66  install_dir = exepath.parent_path().parent_path().string();
67  else
68  install_dir = exepath.parent_path().string();
69 
70  return install_dir;
71 }
72 
73 void System::_init(const String &install_directory) {
74 
75  if (install_directory.empty()) {
76  install_dir = _locate_install_dir(proc_info().exe.c_str());
77  }
78  else {
79  // set installation directory
80  install_dir = install_directory;
81  while (boost::ends_with(install_dir, "/"))
82  install_dir = install_dir.substr(0, install_dir.length()-1);
83  }
84 
85  cluster_def.reset(new ClusterDefinition::ClusterDefinition(install_dir + "/conf/cluster.def"));
86 
87  if (exe_name.empty())
88  exe_name = Path(proc_info().args[0]).filename().generic_string();
89 
90  // initialize logging system
91  Logger::initialize(exe_name);
92 }
93 
94 void System::initialize_tm(struct tm *tmval) {
95  memset(tmval, 0, sizeof(struct tm));
96  tmval->tm_isdst = -1;
97 #if !defined(__sun__)
98  tmval->tm_gmtoff = System::tm_gmtoff;
99  tmval->tm_zone = (char *)System::tm_zone.c_str();
100 #endif
101 }
102 
104  return cpu_info().total_cores;
105 }
106 
107 namespace {
108  int32_t drive_count = 0;
109 }
110 
112  if (drive_count > 0)
113  return drive_count;
114 
115 #if defined(__linux__)
116  String device_regex = "sd[a-z][a-z]?|hd[a-z][a-z]?";
117 #elif defined(__APPLE__)
118  String device_regex = "disk[0-9][0-9]?";
119 #else
120  ImplementMe;
121 #endif
122 
123  vector<struct dirent> listing;
124 
125  FileUtils::readdir("/dev", device_regex, listing);
126 
127  drive_count = listing.size();
128 
129  return drive_count;
130 }
131 
132 int32_t System::get_pid() {
133  return proc_info().pid;
134 }
static std::mutex mutex
Definition: Logger.cc:43
void initialize(const String &name)
Public initialization function - creates a singleton instance of LogWriter.
Definition: Logger.cc:45
Path parent_path() const
Definition: Path.h:61
static String filename
Definition: Config.cc:48
static bool ms_initialized
true if initialize was already called
Definition: System.h:184
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Compatibility class for boost::filesystem::path.
static String tm_zone
Timezone abbreviation.
Definition: System.h:126
STL namespace.
static void initialize_tm(struct tm *tmval)
Initialize struct tm.
Definition: System.cc:94
String filename() const
Definition: Path.h:62
static int32_t get_processor_count()
The processor count.
Definition: System.cc:103
static long tm_gmtoff
Seconds east of UTC.
Definition: System.h:123
Compatibility class for boost::filesystem::path.
Definition: Path.h:45
ImplementMe
Definition: IOHandler.cc:440
File system utility functions.
static String exe_name
The exe file name.
Definition: System.h:120
Logging routines and macros.
Compatibility Macros for C/C++.
static int32_t get_drive_count()
Returns the number of drives.
Definition: System.cc:111
static std::mutex ms_mutex
a Mutex to protect the static members
Definition: System.h:187
Hypertable definitions
static void readdir(const String &dirname, const String &fname_regex, std::vector< struct dirent > &listing)
Reads all directory entries, applies a regular expression and returns those which match...
Definition: FileUtils.cc:511
static String locate_install_dir(const char *argv0)
Returns the installation directory.
Definition: System.cc:50
static String install_dir
The installation directory.
Definition: System.h:114
static void _init(const String &install_directory)
Internal initialization helper.
Definition: System.cc:73
static std::unique_ptr< ClusterDefinition::ClusterDefinition > cluster_def
Cluster definition.
Definition: System.h:117
static int32_t get_pid()
The pid of the current process.
Definition: System.cc:132
System information and statistics based on libsigar.
static String _locate_install_dir(const char *argv0)
Returns the installation directory; same as locate_install_dir but does not lock ms_mutex.
Definition: System.cc:55