1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/* Ergo, version 3.8.2, a program for linear scaling electronic structure
* calculations.
* Copyright (C) 2023 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
* and Anastasia Kruchinina.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Primary academic reference:
* Ergo: An open-source program for linear-scaling electronic structure
* calculations,
* Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
* Kruchinina,
* SoftwareX 7, 107 (2018),
* <http://dx.doi.org/10.1016/j.softx.2018.03.005>
*
* For further information about Ergo, see <http://www.ergoscf.org>.
*/
/** @file utilities.cc
@brief Basic OS access utilities.
@author: Elias Rudberg <em>responsible</em>
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <unistd.h>
#include "utilities.h"
#include "output.h"
static int unique_random_filename_counter = 0;
int
generate_unique_random_filename(char* result, unsigned n)
{
char s[888];
unique_random_filename_counter++;
sprintf(s, "random_unique_filename_%i_%i_%i", unique_random_filename_counter, getpid(), rand());
if(strlen(s) >= n)
{
memset(result, 0, n);
return -1;
}
strcpy(result, s);
return 0;
}
long int get_file_size(const char* fileName) {
FILE * fptr = fopen ( fileName , "rb" );
if ( fptr == NULL )
return -1;
fseek( fptr, (long int)0, SEEK_END );
long int sz = ftell( fptr );
fclose ( fptr );
return sz;
}
void
get_host_name(host_name_struct* result)
{
if(result == NULL)
return;
memset(result, 0, sizeof(host_name_struct));
gethostname(result->s, sizeof(result->s)-1);
}
/* FIXME use getwd() system call instead. */
void
get_working_directory(working_directory_struct* result)
{
#if 0
int noOfBytesRead, i;
char fileName[888];
char cmdString[888];
FILE* f;
if(result == NULL)
return;
memset(result, 0, sizeof(working_directory_struct));
generate_unique_random_filename(fileName, 888);
sprintf(cmdString, "pwd > %s", fileName);
if (system(cmdString) != 0)
return;
f = fopen(fileName, "rt");
if(f == NULL)
return;
noOfBytesRead = (int)fread(result->s, 1, MAX_WORKING_DIRECTORY_LEN, f);
fclose(f);
if(noOfBytesRead <= 0)
return;
if(noOfBytesRead >= MAX_WORKING_DIRECTORY_LEN)
return;
/* remove newline */
for(i = 0; i < noOfBytesRead; i++)
{
if(result->s[i] == '\n')
result->s[i] = '\0';
}
/* remove temp file */
sprintf(cmdString, "rm %s", fileName);
if (system(cmdString) != 0)
return;
#else
if(result == NULL)
return;
if (getcwd(result->s, MAX_WORKING_DIRECTORY_LEN) == 0)
return;
#endif
}
int
get_memory_usage_by_ps(double* virtualMemoryGigaBytes, double* residentMemoryGigaBytes)
{
char fileName[444];
char cmdString[888];
char* p;
int pid;
const unsigned BufSize = 444;
char buf[BufSize];
FILE* f;
size_t noOfBytesRead;
if(virtualMemoryGigaBytes == NULL || residentMemoryGigaBytes == NULL)
return -1;
*virtualMemoryGigaBytes = 0;
*residentMemoryGigaBytes = 0;
generate_unique_random_filename(fileName, 444);
pid = getpid();
sprintf(cmdString, "ps -p %i -o vsize,rss | tail -n 1 > %s", pid, fileName);
if (system(cmdString) != 0)
return -1;
f = fopen(fileName, "rt");
if(f == NULL)
return -1;
memset(buf, 0, BufSize);
noOfBytesRead = fread(buf, 1, BufSize, f);
fclose(f);
if(noOfBytesRead <= 0)
return -1;
if(noOfBytesRead >= BufSize)
return -1;
p = buf;
/* printf("get_memory_usage, buf = '%s'\n", buf); */
*virtualMemoryGigaBytes = atof(p) / 1000000;
while(*p != ' ' && *p != '\0')
p++;
/* skip blanks */
while(*p == ' ')
p++;
*residentMemoryGigaBytes = atof(p) / 1000000;
/* remove temp file */
sprintf(cmdString, "rm %s", fileName);
if (system(cmdString) != -1)
return -1;
return 0;
}
static int getNumberFromBuffer(const char* buffer, const char* s)
{
const char* p = buffer;
int slen = strlen(s);
while(1)
{
if(*p == '\0')
return -11;
/* now p points to the beginning of a new line. */
if(memcmp(p, s, slen) == 0)
{
int number;
/* string found! */
/* skip until blank or tab */
while(*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
p++;
/* skip blanks and tabs */
while(*p == ' ' || *p == '\t')
p++;
/* get number */
number = atoi(p);
/* skip until blank or tab */
while(*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
p++;
/* skip blanks and tabs */
while(*p == ' ' || *p == '\t')
p++;
/* now p should point to "kB" */
if(memcmp(p, "kB", 2) != 0)
return -22;
return number;
}
/* skip to next line */
while(*p != '\n' && *p != '\0')
p++;
p++;
}
return -33;
}
#define PROCFILESIZE 8888
int
get_memory_usage_by_procfile(double* virtualMemGigaBytes,
double* residentMemGigaBytes,
double* virtualMemPeakGigaBytes)
{
char fileName[888];
char buffer[PROCFILESIZE];
int pid;
size_t noOfBytesRead;
FILE* f;
int VmSize_kB, VmRSS_kB, VmPeak_kB;
if(virtualMemGigaBytes == NULL || residentMemGigaBytes == NULL || virtualMemPeakGigaBytes == NULL)
return -1;
*virtualMemGigaBytes = 0;
*residentMemGigaBytes = 0;
*virtualMemPeakGigaBytes = 0;
pid = getpid();
sprintf(fileName, "/proc/%i/status", pid);
memset(buffer, 0, PROCFILESIZE);
f = fopen(fileName, "rt");
if(f == NULL)
return -1;
noOfBytesRead = fread(buffer, 1, PROCFILESIZE, f);
fclose(f);
if(noOfBytesRead <= 0)
return -1;
if(noOfBytesRead >= PROCFILESIZE)
return -1;
VmSize_kB = getNumberFromBuffer(buffer, "VmSize:");
VmRSS_kB = getNumberFromBuffer(buffer, "VmRSS:");
VmPeak_kB = getNumberFromBuffer(buffer, "VmPeak:");
if(VmSize_kB <= 0 || VmRSS_kB <= 0)
{
do_output(LOG_CAT_ERROR, LOG_AREA_UNDEFINED,
"error getting VmSize_kB or VmRSS_kB. Values returned: "
"VmSize_kB = %i, VmRSS_kB = %i",
VmSize_kB, VmRSS_kB);
return -1;
}
*virtualMemGigaBytes = (double)VmSize_kB / 1000000;
*residentMemGigaBytes = (double)VmRSS_kB / 1000000;
if(VmPeak_kB > 0)
*virtualMemPeakGigaBytes = (double)VmPeak_kB / 1000000;
else
*virtualMemPeakGigaBytes = 0;
return 0;
}
#undef PROCFILESIZE
|