[2412] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
[2411] | 3 | |
---|
[2412] | 4 | # this must come first |
---|
| 5 | from __future__ import print_function, unicode_literals, division |
---|
[2411] | 6 | |
---|
[2412] | 7 | # standard library imports |
---|
| 8 | from argparse import ArgumentParser |
---|
[2413] | 9 | import json |
---|
| 10 | import shutil |
---|
| 11 | import os |
---|
[2412] | 12 | import os.path |
---|
[2413] | 13 | import subprocess |
---|
| 14 | # import datetime as dt |
---|
[2411] | 15 | |
---|
[2413] | 16 | # Application library imports |
---|
[2460] | 17 | from libconso import * |
---|
[2411] | 18 | |
---|
[2413] | 19 | |
---|
[2412] | 20 | ######################################## |
---|
[2413] | 21 | def get_storedir(login): |
---|
[2411] | 22 | |
---|
[2437] | 23 | print("get_storedir") |
---|
| 24 | |
---|
[2413] | 25 | command = ["ccc_home", "-A", "-u", login] |
---|
| 26 | try : |
---|
| 27 | res = subprocess.check_output(command) |
---|
[2437] | 28 | print("res", res) |
---|
[2421] | 29 | except Exception as rc: |
---|
[2437] | 30 | print("exception", rc) |
---|
[2413] | 31 | res = None |
---|
| 32 | |
---|
[2424] | 33 | return res.strip() |
---|
[2413] | 34 | |
---|
| 35 | |
---|
| 36 | ######################################## |
---|
| 37 | def get_dirsize(dirname): |
---|
| 38 | |
---|
| 39 | command = ["du", "-sbh", dirname] |
---|
| 40 | try : |
---|
[2421] | 41 | res = subprocess.check_output(command) |
---|
| 42 | res = res.split()[0] |
---|
[2413] | 43 | except Exception as rc : |
---|
| 44 | print(rc) |
---|
| 45 | res = None |
---|
| 46 | |
---|
| 47 | return res |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | # ######################################## |
---|
| 51 | # def get_dirlist(dirname): |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | # return output |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | ######################################## |
---|
[2460] | 58 | def parse_myproject(filename, project_name): |
---|
[2413] | 59 | |
---|
| 60 | project = {} |
---|
[2460] | 61 | project["name"] = project_name |
---|
[2412] | 62 | logins = {} |
---|
[2411] | 63 | |
---|
[2413] | 64 | if where_we_run() == "curie": |
---|
| 65 | try : |
---|
| 66 | res = subprocess.check_output("ccc_myproject") |
---|
| 67 | except Exception as rc : |
---|
| 68 | print(rc) |
---|
[2426] | 69 | exit(1) |
---|
[2421] | 70 | with open(os.path.join(DIR["DATA"], OUT["CCCMP"]), "w") as fileout: |
---|
[2413] | 71 | fileout.write(res) |
---|
| 72 | |
---|
[2412] | 73 | with open(filename, "r") as filein: |
---|
| 74 | # Skip lines until we find project name. |
---|
| 75 | # Then extract script date |
---|
| 76 | for ligne in filein: |
---|
[2460] | 77 | if project["name"] in ligne: |
---|
[2412] | 78 | today = ligne.split()[-1] |
---|
[2413] | 79 | today = string_to_date(today) |
---|
[2460] | 80 | project["machine"] = ligne.split()[5] |
---|
| 81 | project["nodes"] = ligne.split()[6] |
---|
[2413] | 82 | |
---|
[2412] | 83 | break |
---|
[2411] | 84 | |
---|
[2412] | 85 | # Skip next two lines : first is blank and second is login titles |
---|
| 86 | for _ in xrange(1): |
---|
| 87 | next(filein) |
---|
[2411] | 88 | |
---|
[2413] | 89 | # Login list, until blank line |
---|
[2412] | 90 | for ligne in filein: |
---|
| 91 | if not ligne.strip(): |
---|
| 92 | break |
---|
| 93 | login, conso = ligne.split() |
---|
| 94 | logins[login] = float(conso) |
---|
[2411] | 95 | |
---|
[2413] | 96 | # Skip until we find consumed time (hours) |
---|
[2412] | 97 | for ligne in filein: |
---|
| 98 | if "Total" in ligne: |
---|
| 99 | total = float(ligne.split()[-1]) |
---|
| 100 | break |
---|
[2411] | 101 | |
---|
[2413] | 102 | # Skip until we find allocated time (hours) |
---|
[2412] | 103 | for ligne in filein: |
---|
| 104 | if "Allocated" in ligne: |
---|
| 105 | project["alloc"] = float(ligne.split()[-1]) |
---|
| 106 | break |
---|
[2411] | 107 | |
---|
[2413] | 108 | # Skip until we find theoratical use (%) |
---|
[2412] | 109 | for ligne in filein: |
---|
| 110 | if "Suggested use at this time" in ligne: |
---|
| 111 | utheo = float(ligne.split()[-1].strip("%")) |
---|
| 112 | break |
---|
[2411] | 113 | |
---|
[2413] | 114 | # Skip until we find real use (%) |
---|
[2412] | 115 | for ligne in filein: |
---|
| 116 | if "Real use at this time" in ligne: |
---|
| 117 | ureal = float(ligne.split()[-1].strip("%")) |
---|
| 118 | break |
---|
[2411] | 119 | |
---|
[2413] | 120 | # Skip until we find deadline |
---|
[2412] | 121 | for ligne in filein: |
---|
| 122 | if "Project deadline" in ligne: |
---|
| 123 | project["deadline"] = ligne.split()[-1] |
---|
| 124 | break |
---|
[2411] | 125 | |
---|
[2413] | 126 | return project, logins, today, total, utheo, ureal |
---|
[2411] | 127 | |
---|
| 128 | |
---|
[2412] | 129 | ######################################## |
---|
| 130 | def write_param(filename, project): |
---|
[2411] | 131 | |
---|
[2412] | 132 | if args.dryrun: |
---|
| 133 | print(json.dumps(project, indent=2)) |
---|
| 134 | else: |
---|
| 135 | with open(filename, "w") as fileout: |
---|
| 136 | json.dump(project, fileout, indent=2) |
---|
[2411] | 137 | |
---|
| 138 | |
---|
[2412] | 139 | ######################################## |
---|
[2460] | 140 | def write_bilan(filename, today, total, ureal, utheo, |
---|
| 141 | run_mean, pen_mean, run_std, pen_std): |
---|
[2412] | 142 | """ |
---|
| 143 | Conso totale par jour |
---|
| 144 | --------------------- |
---|
| 145 | on garde le total, date en tete en accumulant dans le fichier : |
---|
| 146 | OUT_CONSO_BILAN |
---|
| 147 | """ |
---|
[2411] | 148 | |
---|
[2460] | 149 | fmt_str = "{:10s} {:12s} {:11s} {:11s} {:13s} {:13s} {:13s} {:13s}\n" |
---|
[2412] | 150 | |
---|
[2460] | 151 | title_str = fmt_str.format( |
---|
| 152 | "date", |
---|
| 153 | "conso(hours)", |
---|
| 154 | "real_use(%)", |
---|
| 155 | "theo_use(%)", |
---|
| 156 | "running(core)", |
---|
| 157 | "pending(core)", |
---|
| 158 | "run_std(core)", |
---|
| 159 | "pen_std(core)", |
---|
| 160 | ) |
---|
| 161 | |
---|
| 162 | fmt_str = ( |
---|
| 163 | "{:%Y-%m-%d} {:12.2f} {:11.2f} {:11.2f} " |
---|
| 164 | "{:13.2f} {:13.2f} {:13.2f} {:13.2f}\n" |
---|
| 165 | ) |
---|
| 166 | |
---|
| 167 | result_str = fmt_str.format( |
---|
| 168 | today, |
---|
| 169 | total, |
---|
| 170 | ureal, |
---|
| 171 | utheo, |
---|
| 172 | run_mean, |
---|
| 173 | pen_mean, |
---|
| 174 | run_std, |
---|
| 175 | pen_std, |
---|
| 176 | ) |
---|
| 177 | |
---|
[2412] | 178 | if args.dryrun: |
---|
| 179 | print(title_str.strip()) |
---|
| 180 | print(result_str.strip()) |
---|
| 181 | else: |
---|
| 182 | if not os.path.isfile(filename): |
---|
| 183 | with open(filename, "w") as fileout: |
---|
| 184 | fileout.write(title_str) |
---|
| 185 | with open(filename, "a") as fileout: |
---|
| 186 | fileout.write(result_str) |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | ######################################## |
---|
| 190 | def write_utheo(filename, today, utheo): |
---|
| 191 | """ |
---|
| 192 | Conso théorique par jour |
---|
| 193 | ------------------------ |
---|
| 194 | OUT_CONSO_THEO |
---|
| 195 | """ |
---|
| 196 | |
---|
| 197 | title_str = "{:10s} {:11s}\n".format( |
---|
| 198 | "date", |
---|
| 199 | "theo_use(%)", |
---|
| 200 | ) |
---|
[2413] | 201 | result_str = "{:%Y-%m-%d} {:11.2f}\n".format( |
---|
[2412] | 202 | today, |
---|
| 203 | utheo, |
---|
| 204 | ) |
---|
| 205 | |
---|
| 206 | if args.dryrun: |
---|
| 207 | print(title_str.strip()) |
---|
| 208 | print(result_str.strip()) |
---|
| 209 | else: |
---|
| 210 | if not os.path.isfile(filename): |
---|
| 211 | with open(filename, "w") as fileout: |
---|
| 212 | fileout.write(title_str) |
---|
| 213 | with open(filename, "a") as fileout: |
---|
| 214 | fileout.write(result_str) |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | ######################################## |
---|
| 218 | def write_login(filename, today, logins): |
---|
| 219 | """ |
---|
| 220 | Conso par login (HOME) |
---|
| 221 | ---------------------- |
---|
| 222 | on garde la trace de chaque login, date en tete, en remplacant |
---|
| 223 | le fichier a chaque fois : OUT_CONSO_LOGIN |
---|
| 224 | """ |
---|
| 225 | |
---|
| 226 | title_str = "{:10s} {:10s} {:12s}\n".format( |
---|
| 227 | "date", |
---|
| 228 | "login", |
---|
| 229 | "conso(hours)", |
---|
| 230 | ) |
---|
| 231 | |
---|
| 232 | with open(filename, "w") as fileout: |
---|
| 233 | if args.dryrun: |
---|
| 234 | print(title_str.strip()) |
---|
| 235 | else: |
---|
| 236 | fileout.write(title_str) |
---|
| 237 | |
---|
| 238 | for key in sorted(logins): |
---|
[2413] | 239 | result_str = "{:%Y-%m-%d} {:10s} {:12.2f}\n".format( |
---|
[2412] | 240 | today, |
---|
| 241 | key, |
---|
| 242 | logins[key], |
---|
| 243 | ) |
---|
| 244 | if args.dryrun: |
---|
| 245 | print(result_str.strip()) |
---|
| 246 | else: |
---|
| 247 | fileout.write(result_str) |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | ######################################## |
---|
[2417] | 251 | def write_store(filename, today, logins): |
---|
[2412] | 252 | """ |
---|
| 253 | volume cree sur STORE |
---|
| 254 | --------------------- |
---|
| 255 | par login qui a consomme, en remplacant le fichier a chaque fois : |
---|
| 256 | OUT_CONSO_STORE |
---|
| 257 | """ |
---|
| 258 | |
---|
[2413] | 259 | items = (login for login, conso in logins.iteritems() |
---|
| 260 | if conso > 0.) |
---|
[2412] | 261 | |
---|
[2417] | 262 | title_str = "{:10s} {:10s} {:>7s} {:s}\n".format( |
---|
| 263 | "date", |
---|
| 264 | "login", |
---|
| 265 | "dirsize", |
---|
| 266 | "dirname", |
---|
| 267 | ) |
---|
[2412] | 268 | |
---|
[2417] | 269 | with open(filename, "w") as fileout: |
---|
| 270 | if args.dryrun: |
---|
| 271 | print(title_str.strip()) |
---|
| 272 | else: |
---|
| 273 | fileout.write(title_str) |
---|
[2413] | 274 | |
---|
[2417] | 275 | for login in items: |
---|
| 276 | if args.verbose: |
---|
| 277 | print(login) |
---|
| 278 | storedir = get_storedir(login) |
---|
| 279 | if not storedir: |
---|
| 280 | break |
---|
| 281 | igcm_out = os.path.join(storedir, "IGCM_OUT") |
---|
[2413] | 282 | |
---|
[2421] | 283 | if not os.path.isdir(igcm_out): |
---|
| 284 | break |
---|
[2417] | 285 | |
---|
[2421] | 286 | dirlist = [] |
---|
| 287 | try: |
---|
| 288 | dirlist = os.listdir(igcm_out) |
---|
| 289 | except OSError as rc: |
---|
| 290 | print("Error on os.listdir({}):\n{}".format(igcm_out, rc)) |
---|
| 291 | |
---|
| 292 | for dirname in dirlist: |
---|
[2417] | 293 | result_str = "{:%Y-%m-%d} {:10s} {:>7s} {:s}\n".format( |
---|
| 294 | today, |
---|
| 295 | login, |
---|
[2421] | 296 | get_dirsize(os.path.join(igcm_out, dirname)), |
---|
| 297 | os.path.join(igcm_out, dirname) |
---|
[2417] | 298 | ) |
---|
[2421] | 299 | |
---|
| 300 | if args.dryrun or args.verbose: |
---|
[2417] | 301 | print(result_str.strip()) |
---|
[2421] | 302 | |
---|
| 303 | if not args.dryrun: |
---|
[2417] | 304 | fileout.write(result_str) |
---|
| 305 | |
---|
| 306 | |
---|
[2412] | 307 | ######################################## |
---|
[2437] | 308 | def save_files(file_list, today): |
---|
[2417] | 309 | |
---|
| 310 | if not args.dryrun: |
---|
| 311 | suffix = "{:%Y%m%d}".format(today) |
---|
[2437] | 312 | for filename in file_list: |
---|
[2417] | 313 | filein = os.path.join(DIR["DATA"], filename) |
---|
| 314 | if os.path.isfile(filein): |
---|
[2425] | 315 | fileout = os.path.join(DIR["SAVEDATA"], |
---|
[2417] | 316 | "_".join((filename, suffix))) |
---|
| 317 | shutil.copy(filein, fileout) |
---|
| 318 | |
---|
| 319 | |
---|
| 320 | ######################################## |
---|
[2412] | 321 | if __name__ == '__main__': |
---|
| 322 | |
---|
| 323 | # Get arguments from command line |
---|
| 324 | # =============================== |
---|
| 325 | parser = ArgumentParser() |
---|
| 326 | parser.add_argument("-v", "--verbose", action="store_true", |
---|
| 327 | help="Verbose mode") |
---|
| 328 | parser.add_argument("-d", "--dryrun", action="store_true", |
---|
| 329 | help="dry run, no file produced") |
---|
| 330 | parser.add_argument("-a", "--all", action="store_false", |
---|
| 331 | help="produce all files (default)") |
---|
| 332 | parser.add_argument("-b", "--bilan", action="store_true", |
---|
| 333 | help="produce all files (default)") |
---|
| 334 | parser.add_argument("-l", "--login", action="store_true", |
---|
| 335 | help="produce all files (default)") |
---|
| 336 | parser.add_argument("-s", "--store", action="store_true", |
---|
| 337 | help="produce all files (default)") |
---|
| 338 | |
---|
| 339 | args = parser.parse_args() |
---|
| 340 | if args.verbose: |
---|
| 341 | print(os.path.basename(__file__)) |
---|
[2413] | 342 | print(where_we_run()) |
---|
[2412] | 343 | print(args) |
---|
| 344 | |
---|
[2417] | 345 | if args.bilan or args.login or args.store: |
---|
| 346 | args.all = False |
---|
[2412] | 347 | |
---|
[2460] | 348 | project_name, DIR, OUT = parse_config("bin/config.ini") |
---|
| 349 | |
---|
[2412] | 350 | if args.verbose: |
---|
[2413] | 351 | print(DIR["DATA"]) |
---|
[2425] | 352 | print(DIR["SAVEDATA"]) |
---|
[2412] | 353 | |
---|
[2460] | 354 | (project, logins, today, total, utheo, ureal) = parse_myproject( |
---|
| 355 | os.path.join(DIR["DATA"], OUT["CCCMP"]), |
---|
| 356 | project_name |
---|
| 357 | ) |
---|
[2412] | 358 | |
---|
| 359 | if args.verbose: |
---|
| 360 | print(today, utheo, ureal) |
---|
| 361 | print(project) |
---|
| 362 | print(logins) |
---|
| 363 | |
---|
| 364 | # Produce files |
---|
| 365 | # ============= |
---|
| 366 | |
---|
| 367 | # 1- Parametres du projet |
---|
| 368 | # ----------------------- |
---|
[2417] | 369 | if args.verbose: |
---|
| 370 | print("=> write_param") |
---|
[2412] | 371 | |
---|
[2417] | 372 | write_param(os.path.join(DIR["DATA"], OUT["PARAM"]), project) |
---|
| 373 | |
---|
[2412] | 374 | # 2- Conso totale par jour |
---|
| 375 | # ------------------------ |
---|
[2437] | 376 | |
---|
[2417] | 377 | if args.verbose: |
---|
| 378 | print("=> write_bilan") |
---|
| 379 | |
---|
[2437] | 380 | file_jobs = get_last_file( |
---|
| 381 | DIR["SAVEDATA"], |
---|
| 382 | "{}_{:%Y%m%d}".format(OUT["JOBS"], today) |
---|
| 383 | ) |
---|
| 384 | if args.verbose: |
---|
| 385 | print(file_jobs) |
---|
| 386 | |
---|
| 387 | run_mean = np.nan |
---|
| 388 | pen_mean = np.nan |
---|
| 389 | run_std = np.nan |
---|
| 390 | pen_std = np.nan |
---|
| 391 | |
---|
| 392 | if file_jobs: |
---|
| 393 | try: |
---|
| 394 | data = np.genfromtxt( |
---|
| 395 | file_jobs, |
---|
| 396 | skip_header=1, |
---|
| 397 | converters={ |
---|
| 398 | 0: string_to_datetime, |
---|
[2460] | 399 | 1: float, |
---|
| 400 | 2: float, |
---|
[2437] | 401 | }, |
---|
| 402 | missing_values="nan", |
---|
| 403 | ) |
---|
| 404 | except Exception as rc: |
---|
| 405 | print("Problem with file {} :\n{}".format(file_jobs, rc)) |
---|
| 406 | exit(1) |
---|
| 407 | |
---|
| 408 | if len(data) == 24: |
---|
| 409 | run_mean = np.nanmean( |
---|
| 410 | np.array([run for _, run, _ in data]) |
---|
| 411 | ) |
---|
| 412 | pen_mean = np.nanmean( |
---|
| 413 | np.array([pen for _, _, pen in data]) |
---|
| 414 | ) |
---|
| 415 | |
---|
| 416 | run_std = np.nanstd( |
---|
| 417 | np.array([run for _, run, _ in data]) |
---|
| 418 | ) |
---|
| 419 | pen_std = np.nanstd( |
---|
| 420 | np.array([pen for _, _, pen in data]) |
---|
| 421 | ) |
---|
| 422 | |
---|
| 423 | if args.verbose: |
---|
| 424 | print(run_mean, pen_mean, run_std, pen_std) |
---|
| 425 | |
---|
[2412] | 426 | write_bilan( |
---|
[2417] | 427 | os.path.join(DIR["DATA"], OUT["BILAN"]), |
---|
[2412] | 428 | today, |
---|
| 429 | total, |
---|
| 430 | ureal, |
---|
[2437] | 431 | utheo, |
---|
| 432 | run_mean, |
---|
| 433 | pen_mean, |
---|
| 434 | run_std, |
---|
| 435 | pen_std, |
---|
[2412] | 436 | ) |
---|
| 437 | |
---|
| 438 | # 2b- Conso théorique par jour |
---|
| 439 | # ---------------------------- |
---|
[2417] | 440 | if args.verbose: |
---|
| 441 | print("=> write_utheo") |
---|
[2412] | 442 | |
---|
[2417] | 443 | write_utheo(os.path.join(DIR["DATA"], OUT["UTHEO"]), today, utheo) |
---|
| 444 | |
---|
[2412] | 445 | # 3- Conso par login (HOME) |
---|
| 446 | # ------------------------- |
---|
[2417] | 447 | if args.verbose: |
---|
| 448 | print("=> write_login") |
---|
[2412] | 449 | |
---|
[2417] | 450 | write_login(os.path.join(DIR["DATA"], OUT["LOGIN"]), today, logins) |
---|
| 451 | |
---|
[2412] | 452 | # 4- volume cree sur STORE |
---|
| 453 | # ------------------------ |
---|
[2417] | 454 | if args.verbose: |
---|
| 455 | print("=> write_store") |
---|
| 456 | |
---|
[2437] | 457 | if where_we_run() == "curie": |
---|
| 458 | write_store(os.path.join(DIR["DATA"], OUT["STORE"]), today, logins) |
---|
[2412] | 459 | |
---|
| 460 | # Save files (on WORKDIR) |
---|
| 461 | # ======================= |
---|
[2417] | 462 | if args.verbose: |
---|
| 463 | print("=> Save files") |
---|
[2412] | 464 | if not args.dryrun: |
---|
[2437] | 465 | file_list = [ |
---|
| 466 | OUT["PARAM"], |
---|
| 467 | OUT["BILAN"], |
---|
| 468 | OUT["UTHEO"], |
---|
| 469 | OUT["LOGIN"], |
---|
| 470 | OUT["STORE"], |
---|
| 471 | OUT["CCCMP"], |
---|
| 472 | ] |
---|
[2426] | 473 | |
---|
[2437] | 474 | save_files(file_list, today) |
---|
| 475 | |
---|
[2426] | 476 | exit(0) |
---|
[2433] | 477 | |
---|