9 #include <boost/filesystem/operations.hpp> 
   10 #include <boost/static_assert.hpp> 
   11 #include <boost/lexical_cast.hpp> 
   24 #include <sys/resource.h> 
   27 #  include <sys/param.h> 
   28 #  include <sys/sysctl.h> 
   29 #  include <sys/user.h> 
   34 #  include <sys/types.h> 
   35 #  include <sys/sysctl.h> 
   36 #  include <mach/task.h> 
   37 #  include <mach/mach_init.h> 
   66   size_t system_memory_use_limit() 
 
   69     MEMORYSTATUSEX statex;
 
   70     statex.dwLength = 
sizeof(statex);
 
   71     GlobalMemoryStatusEx(&statex);
 
   72     return statex.ullTotalPhys; 
 
   77       if (getrlimit(RLIMIT_AS, &rlp) == 0
 
   79         limit_by_rlimit = rlp.rlim_cur;
 
   81         limit_by_rlimit *= 1024;
 
   83         std::cerr << 
"rlimit " << limit_by_rlimit << 
"\n";
 
   90       size_t len=
sizeof(usermem);
 
   93       if (sysctl(mib, 2, &usermem, &len, NULL, 0) == 0
 
   94           && len == 
sizeof(usermem)) {
 
   95         std::cerr << 
"usermem " << usermem << std::endl;
 
   96         return std::min((
size_t)usermem, limit_by_rlimit);
 
  101       std::string name, unit;
 
  103       std::ifstream is(
"/proc/meminfo");
 
  104       if (is >> name >> value >> unit
 
  105           && name == 
"MemTotal:" && unit == 
"kB")
 
  106         return std::min(value * 1024, limit_by_rlimit);
 
  108 #if (defined __FreeBSD__) 
  109     const long mem = sysconf(_SC_PHYS_PAGES);
 
  111       return std::min(mem * getpagesize(), limit_by_rlimit);
 
  130     std::cerr << 
"ncpu " << ncpu << 
" > " << 
"MaxThreads " << 
MaxThreads << 
"\n";
 
  161   return usi_mode_silent;
 
  165   usi_mode_silent = enable;
 
  169   return search_exact_value_in_one_reply;
 
  173   search_exact_value_in_one_reply = enable;
 
  188   std::cerr << 
"using " << home << 
" as OSL_HOME, word size " 
  199   return boost::filesystem::exists(dir)
 
  200     && boost::filesystem::is_directory(dir);
 
  205   if (isGoodDir(candidate))
 
  211     std::cerr << 
"skipping " << candidate << std::endl;
 
  218     trySetDir(result, first_try);
 
  220   if (
const char *env = getenv(
"GPSSHOGI_HOME"))
 
  221     trySetDir(result, env);
 
  223 #if defined GPSSHOGI_HOME 
  225     trySetDir(result, GPSSHOGI_HOME);
 
  229     if (
const char *env = getenv(
"OSL_HOME"))
 
  230       trySetDir(result, env);
 
  242   static const std::string home_directory = makeHome(init);
 
  243   return home_directory;
 
  248   return home().c_str();
 
  256 #ifdef OSL_PUBLIC_RELEASE 
  258   if (
const char *env = getenv(
"HOME"))
 
  259     return std::string(env) + 
"/gpsusi.conf";
 
  260   if (
const char *env = getenv(
"USERPROFILE"))
 
  261     return std::string(env) + 
"/gpsusi.conf";
 
  264   static const std::string home_directory = makeHome();
 
  265   return home_directory + 
"/gpsusi.conf";
 
  270   static const int value = getenv(
"OSL_RESIGN_VALUE") 
 
  271     ? atoi(getenv(
"OSL_RESIGN_VALUE")) : 0;
 
  272   return (value > 0) ? value : 10000;
 
  278   if (
const char *env = getenv(
"OSL_TEST"))
 
  279     trySetDir(result, env);
 
  282     result = home() + 
"/data";  
 
  284   std::cerr << 
"using " << result << 
" as OSL_TEST" << std::endl;
 
  291   if (
const char *env = getenv(
"OSL_TEST_PUBLIC"))
 
  292     trySetDir(result, env);
 
  295     result = home() + 
"/public-domain";
 
  297   std::cerr << 
"using " << result << 
" as OSL_TEST_PUBLIC" << std::endl;
 
  303   static const std::string test_directory = makeTest();
 
  304   return test_directory;
 
  309   static const std::string test_directory = makeTestPublic();
 
  310   return test_directory;
 
  315   struct NameHolder : std::map<std::string,std::string>
 
  317     std::string directory;
 
  319     NameHolder(
const std::string& d) : directory(d)
 
  324     iterator add(
const std::string& key, 
const std::string& value)
 
  326       return insert(std::make_pair(key, value)).first;
 
  328     iterator addRelative(
const std::string& key, 
const std::string& filename)
 
  330       std::string value = directory + filename;
 
  331       return add(key, value);
 
  333     iterator addRelative(
const std::string& filename)
 
  335       return addRelative(filename, filename);
 
  342   static NameHolder table(testPrivate());
 
  343   NameHolder::iterator p=table.find(filename);
 
  344   if (p == table.end()) {
 
  345     p = table.addRelative(filename);
 
  347   return p->second.c_str();
 
  352   static NameHolder table(testPublic());
 
  353   NameHolder::iterator p=table.find(filename);
 
  354   if (p == table.end()) {
 
  355     p = table.addRelative(filename);
 
  357   return p->second.c_str();
 
  362   static NameHolder table(testPublic()+
"/floodgate2010");
 
  363   NameHolder::iterator p=table.find(filename);
 
  364   if (p == table.end()) {
 
  365     p = table.addRelative(filename);
 
  367   return p->second.c_str();
 
  372   static NameHolder table(home()+
"/data");
 
  373   NameHolder::iterator p=table.find(filename);
 
  374   if (p == table.end()) {
 
  375     if (! filename.empty() && filename[0] == 
'/') {
 
  377       p = table.add(filename, filename);
 
  381       p = table.addRelative(filename, 
 
  382                             (filename == 
"" ? 
"joseki.dat" : filename));
 
  385   return p->second.c_str();
 
  392   static const DWORD process_id = GetCurrentProcessId();
 
  393   HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
 
  395   if (NULL == hProcess)
 
  397     std::cerr << 
"Failed to get residentMemoryUse()\n";
 
  401   size_t working_set = 0;
 
  402   PROCESS_MEMORY_COUNTERS pmc;
 
  403   if (GetProcessMemoryInfo(hProcess, &pmc, 
sizeof(pmc))) {
 
  404     working_set = pmc.WorkingSetSize; 
 
  406   CloseHandle(hProcess);
 
  411   std::ifstream is(
"/proc/self/statm");
 
  412   size_t total, resident;
 
  413   if (is >> total >> resident)
 
  414     return resident*getpagesize();
 
  416   mach_msg_type_number_t 
count = TASK_BASIC_INFO_64_COUNT;
 
  417   task_basic_info_64 ti;
 
  418   if (task_info(current_task(), TASK_BASIC_INFO_64, (task_info_t)&ti, &count)
 
  420     return ti.resident_size;
 
  423   static kvm_t *kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, 
"osl kvm_open");
 
  425   kinfo_proc *pp = kvm_getprocs(kd, KERN_PROC_PID, getpid(), &nproc);
 
  427     return pp->ki_rssize * getpagesize();
 
  445   std::cerr << 
"health check\n";
 
  450     std::cerr << 
"loading " << filename << 
' ';
 
  452     std::cerr << (success ? 
"success" : 
"failed\a") << 
"\n";
 
  454       std::cerr << 
"exists?  " << boost::filesystem::exists(filename.c_str()) << 
"\n";
 
  455       std::cerr << 
"regular? " << boost::filesystem::is_regular_file(filename.c_str()) << 
"\n";
 
  461     std::cerr << 
"loading " << filename << 
' ';
 
  463     std::cerr << (success ? 
"success" : 
"failed\a") << 
"\n";
 
  465       std::cerr << 
"exists?  " << boost::filesystem::exists(filename.c_str()) << 
"\n";
 
  466       std::cerr << 
"regular? " << boost::filesystem::is_regular_file(filename.c_str()) << 
"\n";
 
  471   setVerbose(old_verbose);
 
  477   return dfpn_max_depth;
 
  481   dfpn_max_depth = new_depth;
 
  487     "wordsize " +boost::lexical_cast<std::string>(
OSL_WORDSIZE)+
""