[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] |
---|
[2412] | 82 | break |
---|
[2411] | 83 | |
---|
[2412] | 84 | # Skip next two lines : first is blank and second is login titles |
---|
| 85 | for _ in xrange(1): |
---|
| 86 | next(filein) |
---|
[2411] | 87 | |
---|
[2413] | 88 | # Login list, until blank line |
---|
[2412] | 89 | for ligne in filein: |
---|
| 90 | if not ligne.strip(): |
---|
| 91 | break |
---|
| 92 | login, conso = ligne.split() |
---|
| 93 | logins[login] = float(conso) |
---|
[2411] | 94 | |
---|
[2413] | 95 | # Skip until we find consumed time (hours) |
---|
[2412] | 96 | for ligne in filein: |
---|
| 97 | if "Total" in ligne: |
---|
| 98 | total = float(ligne.split()[-1]) |
---|
| 99 | break |
---|
[2411] | 100 | |
---|
[2413] | 101 | # Skip until we find allocated time (hours) |
---|
[2412] | 102 | for ligne in filein: |
---|
| 103 | if "Allocated" in ligne: |
---|
| 104 | project["alloc"] = float(ligne.split()[-1]) |
---|
| 105 | break |
---|
[2411] | 106 | |
---|
[2413] | 107 | # Skip until we find theoratical use (%) |
---|
[2412] | 108 | for ligne in filein: |
---|
| 109 | if "Suggested use at this time" in ligne: |
---|
| 110 | utheo = float(ligne.split()[-1].strip("%")) |
---|
| 111 | break |
---|
[2411] | 112 | |
---|
[2413] | 113 | # Skip until we find real use (%) |
---|
[2412] | 114 | for ligne in filein: |
---|
| 115 | if "Real use at this time" in ligne: |
---|
| 116 | ureal = float(ligne.split()[-1].strip("%")) |
---|
| 117 | break |
---|
[2411] | 118 | |
---|
[2413] | 119 | # Skip until we find deadline |
---|
[2412] | 120 | for ligne in filein: |
---|
| 121 | if "Project deadline" in ligne: |
---|
| 122 | project["deadline"] = ligne.split()[-1] |
---|
| 123 | break |
---|
[2411] | 124 | |
---|
[2413] | 125 | return project, logins, today, total, utheo, ureal |
---|
[2411] | 126 | |
---|
| 127 | |
---|
[2412] | 128 | ######################################## |
---|
| 129 | def write_param(filename, project): |
---|
[2411] | 130 | |
---|
[2412] | 131 | if args.dryrun: |
---|
| 132 | print(json.dumps(project, indent=2)) |
---|
| 133 | else: |
---|
| 134 | with open(filename, "w") as fileout: |
---|
| 135 | json.dump(project, fileout, indent=2) |
---|
[2411] | 136 | |
---|
| 137 | |
---|
[2412] | 138 | ######################################## |
---|
[2460] | 139 | def write_bilan(filename, today, total, ureal, utheo, |
---|
| 140 | run_mean, pen_mean, run_std, pen_std): |
---|
[2412] | 141 | """ |
---|
| 142 | Conso totale par jour |
---|
| 143 | --------------------- |
---|
| 144 | on garde le total, date en tete en accumulant dans le fichier : |
---|
| 145 | OUT_CONSO_BILAN |
---|
| 146 | """ |
---|
[2411] | 147 | |
---|
[2460] | 148 | fmt_str = "{:10s} {:12s} {:11s} {:11s} {:13s} {:13s} {:13s} {:13s}\n" |
---|
[2412] | 149 | |
---|
[2462] | 150 | title_str = fmt_str.format( |
---|
[2460] | 151 | "date", |
---|
| 152 | "conso(hours)", |
---|
| 153 | "real_use(%)", |
---|
| 154 | "theo_use(%)", |
---|
| 155 | "running(core)", |
---|
| 156 | "pending(core)", |
---|
| 157 | "run_std(core)", |
---|
| 158 | "pen_std(core)", |
---|
| 159 | ) |
---|
| 160 | |
---|
| 161 | fmt_str = ( |
---|
| 162 | "{:%Y-%m-%d} {:12.2f} {:11.2f} {:11.2f} " |
---|
| 163 | "{:13.2f} {:13.2f} {:13.2f} {:13.2f}\n" |
---|
| 164 | ) |
---|
| 165 | |
---|
| 166 | result_str = fmt_str.format( |
---|
| 167 | today, |
---|
| 168 | total, |
---|
| 169 | ureal, |
---|
| 170 | utheo, |
---|
| 171 | run_mean, |
---|
| 172 | pen_mean, |
---|
| 173 | run_std, |
---|
| 174 | pen_std, |
---|
| 175 | ) |
---|
| 176 | |
---|
[2412] | 177 | if args.dryrun: |
---|
| 178 | print(title_str.strip()) |
---|
| 179 | print(result_str.strip()) |
---|
| 180 | else: |
---|
| 181 | if not os.path.isfile(filename): |
---|
| 182 | with open(filename, "w") as fileout: |
---|
| 183 | fileout.write(title_str) |
---|
| 184 | with open(filename, "a") as fileout: |
---|
| 185 | fileout.write(result_str) |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | ######################################## |
---|
| 189 | def write_utheo(filename, today, utheo): |
---|
| 190 | """ |
---|
| 191 | Conso théorique par jour |
---|
| 192 | ------------------------ |
---|
| 193 | OUT_CONSO_THEO |
---|
| 194 | """ |
---|
| 195 | |
---|
| 196 | title_str = "{:10s} {:11s}\n".format( |
---|
| 197 | "date", |
---|
| 198 | "theo_use(%)", |
---|
| 199 | ) |
---|
[2413] | 200 | result_str = "{:%Y-%m-%d} {:11.2f}\n".format( |
---|
[2412] | 201 | today, |
---|
| 202 | utheo, |
---|
| 203 | ) |
---|
| 204 | |
---|
| 205 | if args.dryrun: |
---|
| 206 | print(title_str.strip()) |
---|
| 207 | print(result_str.strip()) |
---|
| 208 | else: |
---|
| 209 | if not os.path.isfile(filename): |
---|
| 210 | with open(filename, "w") as fileout: |
---|
| 211 | fileout.write(title_str) |
---|
| 212 | with open(filename, "a") as fileout: |
---|
| 213 | fileout.write(result_str) |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | ######################################## |
---|
| 217 | def write_login(filename, today, logins): |
---|
| 218 | """ |
---|
| 219 | Conso par login (HOME) |
---|
| 220 | ---------------------- |
---|
| 221 | on garde la trace de chaque login, date en tete, en remplacant |
---|
| 222 | le fichier a chaque fois : OUT_CONSO_LOGIN |
---|
| 223 | """ |
---|
| 224 | |
---|
| 225 | title_str = "{:10s} {:10s} {:12s}\n".format( |
---|
| 226 | "date", |
---|
| 227 | "login", |
---|
| 228 | "conso(hours)", |
---|
| 229 | ) |
---|
| 230 | |
---|
| 231 | with open(filename, "w") as fileout: |
---|
| 232 | if args.dryrun: |
---|
| 233 | print(title_str.strip()) |
---|
| 234 | else: |
---|
| 235 | fileout.write(title_str) |
---|
| 236 | |
---|
| 237 | for key in sorted(logins): |
---|
[2413] | 238 | result_str = "{:%Y-%m-%d} {:10s} {:12.2f}\n".format( |
---|
[2412] | 239 | today, |
---|
| 240 | key, |
---|
| 241 | logins[key], |
---|
| 242 | ) |
---|
| 243 | if args.dryrun: |
---|
| 244 | print(result_str.strip()) |
---|
| 245 | else: |
---|
| 246 | fileout.write(result_str) |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | ######################################## |
---|
[2417] | 250 | def write_store(filename, today, logins): |
---|
[2412] | 251 | """ |
---|
| 252 | volume cree sur STORE |
---|
| 253 | --------------------- |
---|
| 254 | par login qui a consomme, en remplacant le fichier a chaque fois : |
---|
| 255 | OUT_CONSO_STORE |
---|
| 256 | """ |
---|
| 257 | |
---|
[2413] | 258 | items = (login for login, conso in logins.iteritems() |
---|
| 259 | if conso > 0.) |
---|
[2412] | 260 | |
---|
[2524] | 261 | title_str = "{:10s} {:10s} {:>7s} {:s}\n".format( |
---|
| 262 | "date", |
---|
| 263 | "login", |
---|
| 264 | "dirsize", |
---|
| 265 | "dirname", |
---|
| 266 | ) |
---|
[2412] | 267 | |
---|
[2417] | 268 | with open(filename, "w") as fileout: |
---|
| 269 | if args.dryrun: |
---|
| 270 | print(title_str.strip()) |
---|
| 271 | else: |
---|
| 272 | fileout.write(title_str) |
---|
[2413] | 273 | |
---|
[2417] | 274 | for login in items: |
---|
| 275 | if args.verbose: |
---|
| 276 | print(login) |
---|
| 277 | storedir = get_storedir(login) |
---|
| 278 | if not storedir: |
---|
[2524] | 279 | print("storedir not found for {}".format(storedir)) |
---|
[2417] | 280 | break |
---|
| 281 | igcm_out = os.path.join(storedir, "IGCM_OUT") |
---|
[2413] | 282 | |
---|
[2421] | 283 | if not os.path.isdir(igcm_out): |
---|
[2524] | 284 | print("no {}".format(igcm_out)) |
---|
| 285 | continue |
---|
[2417] | 286 | |
---|
[2421] | 287 | dirlist = [] |
---|
| 288 | try: |
---|
| 289 | dirlist = os.listdir(igcm_out) |
---|
| 290 | except OSError as rc: |
---|
| 291 | print("Error on os.listdir({}):\n{}".format(igcm_out, rc)) |
---|
| 292 | |
---|
| 293 | for dirname in dirlist: |
---|
[2417] | 294 | result_str = "{:%Y-%m-%d} {:10s} {:>7s} {:s}\n".format( |
---|
| 295 | today, |
---|
| 296 | login, |
---|
[2421] | 297 | get_dirsize(os.path.join(igcm_out, dirname)), |
---|
| 298 | os.path.join(igcm_out, dirname) |
---|
[2417] | 299 | ) |
---|
[2421] | 300 | |
---|
| 301 | if args.dryrun or args.verbose: |
---|
[2417] | 302 | print(result_str.strip()) |
---|
[2421] | 303 | |
---|
| 304 | if not args.dryrun: |
---|
[2417] | 305 | fileout.write(result_str) |
---|
| 306 | |
---|
| 307 | |
---|
[2412] | 308 | ######################################## |
---|
[2437] | 309 | def save_files(file_list, today): |
---|
[2417] | 310 | |
---|
| 311 | if not args.dryrun: |
---|
| 312 | suffix = "{:%Y%m%d}".format(today) |
---|
[2437] | 313 | for filename in file_list: |
---|
[2417] | 314 | filein = os.path.join(DIR["DATA"], filename) |
---|
| 315 | if os.path.isfile(filein): |
---|
[2425] | 316 | fileout = os.path.join(DIR["SAVEDATA"], |
---|
[2417] | 317 | "_".join((filename, suffix))) |
---|
| 318 | shutil.copy(filein, fileout) |
---|
| 319 | |
---|
| 320 | |
---|
| 321 | ######################################## |
---|
[2412] | 322 | if __name__ == '__main__': |
---|
| 323 | |
---|
| 324 | # Get arguments from command line |
---|
| 325 | # =============================== |
---|
| 326 | parser = ArgumentParser() |
---|
| 327 | parser.add_argument("-v", "--verbose", action="store_true", |
---|
| 328 | help="Verbose mode") |
---|
| 329 | parser.add_argument("-d", "--dryrun", action="store_true", |
---|
| 330 | help="dry run, no file produced") |
---|
| 331 | parser.add_argument("-a", "--all", action="store_false", |
---|
| 332 | help="produce all files (default)") |
---|
| 333 | parser.add_argument("-b", "--bilan", action="store_true", |
---|
| 334 | help="produce all files (default)") |
---|
| 335 | parser.add_argument("-l", "--login", action="store_true", |
---|
| 336 | help="produce all files (default)") |
---|
| 337 | parser.add_argument("-s", "--store", action="store_true", |
---|
| 338 | help="produce all files (default)") |
---|
| 339 | |
---|
| 340 | args = parser.parse_args() |
---|
| 341 | if args.verbose: |
---|
| 342 | print(os.path.basename(__file__)) |
---|
[2413] | 343 | print(where_we_run()) |
---|
[2412] | 344 | print(args) |
---|
| 345 | |
---|
[2417] | 346 | if args.bilan or args.login or args.store: |
---|
| 347 | args.all = False |
---|
[2412] | 348 | |
---|
[2460] | 349 | project_name, DIR, OUT = parse_config("bin/config.ini") |
---|
| 350 | |
---|
[2412] | 351 | if args.verbose: |
---|
[2413] | 352 | print(DIR["DATA"]) |
---|
[2425] | 353 | print(DIR["SAVEDATA"]) |
---|
[2412] | 354 | |
---|
[2460] | 355 | (project, logins, today, total, utheo, ureal) = parse_myproject( |
---|
| 356 | os.path.join(DIR["DATA"], OUT["CCCMP"]), |
---|
| 357 | project_name |
---|
| 358 | ) |
---|
[2412] | 359 | |
---|
| 360 | if args.verbose: |
---|
| 361 | print(today, utheo, ureal) |
---|
| 362 | print(project) |
---|
| 363 | print(logins) |
---|
| 364 | |
---|
| 365 | # Produce files |
---|
| 366 | # ============= |
---|
| 367 | |
---|
| 368 | # 1- Parametres du projet |
---|
| 369 | # ----------------------- |
---|
[2417] | 370 | if args.verbose: |
---|
| 371 | print("=> write_param") |
---|
[2412] | 372 | |
---|
[2417] | 373 | write_param(os.path.join(DIR["DATA"], OUT["PARAM"]), project) |
---|
| 374 | |
---|
[2412] | 375 | # 2- Conso totale par jour |
---|
| 376 | # ------------------------ |
---|
[2437] | 377 | |
---|
[2417] | 378 | if args.verbose: |
---|
| 379 | print("=> write_bilan") |
---|
| 380 | |
---|
[2437] | 381 | file_jobs = get_last_file( |
---|
| 382 | DIR["SAVEDATA"], |
---|
| 383 | "{}_{:%Y%m%d}".format(OUT["JOBS"], today) |
---|
| 384 | ) |
---|
| 385 | if args.verbose: |
---|
| 386 | print(file_jobs) |
---|
| 387 | |
---|
| 388 | run_mean = np.nan |
---|
| 389 | pen_mean = np.nan |
---|
| 390 | run_std = np.nan |
---|
| 391 | pen_std = np.nan |
---|
| 392 | |
---|
| 393 | if file_jobs: |
---|
| 394 | try: |
---|
| 395 | data = np.genfromtxt( |
---|
| 396 | file_jobs, |
---|
| 397 | skip_header=1, |
---|
| 398 | converters={ |
---|
| 399 | 0: string_to_datetime, |
---|
[2460] | 400 | 1: float, |
---|
| 401 | 2: float, |
---|
[2437] | 402 | }, |
---|
| 403 | missing_values="nan", |
---|
| 404 | ) |
---|
| 405 | except Exception as rc: |
---|
| 406 | print("Problem with file {} :\n{}".format(file_jobs, rc)) |
---|
| 407 | exit(1) |
---|
| 408 | |
---|
[2524] | 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 | ) |
---|
[2437] | 415 | |
---|
[2524] | 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 | ) |
---|
[2437] | 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 | |
---|