Example #1
0
def printjobs(jobs, wide=False, long=False, title=None,
              header=True, file=sys.stdout):
    """list the jobs"""
    if len(jobs) == 0:
        return
    sumjob = not isinstance(jobs[0]["jobid"], str)
    if long:
        for job in jobs:
            printjoblong(job, sumjob=sumjob, file=file)
        return
    # begin output
    whoami = os.getenv("USER")
    namelen = max(map(len, (job["job_name"] for job in jobs)))
    lens = {
        "title": 10,
        "jobid": 10,
        "name": min(20, max(6, namelen + 1)),
        "stat": 6,
        "user": 10,
        "time": 12
    }
    if sumjob:
        lens["stat"] = 12
    else:
        if any(job["jobid"][-1] == "]" for job in jobs):
            lens["jobid"] = 14
    if wide:
        lens["title"] = 20
        lens["name"] = max(6, namelen + 1)
        lens["queue"] = 8
        lens["project"] = 8
        lens["prio."] = 6
    # header
    if header:
        h = ""
        if sumjob and "title" in jobs[0]:
            h += "group".ljust(lens["title"])
        if not sumjob:
            h += "jobid".ljust(lens["jobid"])
        h += "".join(n.ljust(lens[n]) for n in ("name", "stat", "user"))
        if wide:
            h += "".join(n.ljust(lens[n]) for n in ("queue", "project"))
            if not sumjob:
                h += "prio.".ljust(lens["prio."])
        if sumjob:
            h += "runtime".rjust(lens["time"])
        else:
            h += "wait/runtime".rjust(lens["time"])
        h += "  resources"
        h = h.upper()
        if title:
            h += "  " + color(title, "b")
        print(h, file=file)
    for job in jobs:
        l = ""
        if sumjob and "title" in job:
            # title
            title = job["title"]
            if not wide:
                if len(title) >= lens["title"]:
                    title = title[:lens["title"] - 2] + "*"
            l += color(title.ljust(lens["title"]), "b")
        if not sumjob:
            # jobid
            l += (job["jobid"] + " ").ljust(lens["jobid"])
        # job name
        jobname = job["job_name"] if job["job_name"] else ""
        if not wide:
            if len(jobname) >= lens["name"]:
                jobname = "*" + jobname[-lens["name"] + 2:]
        l += jobname.ljust(lens["name"])
        # status
        if sumjob:
            l += color("%3d " % job["stat"]["PEND"], "r")
            l += color("%3d " % job["stat"]["RUN"], "g")
            done = job["stat"]["EXIT"] + job["stat"]["DONE"]
            if done:
                l += color("%3d " % done, "y")
            else:
                l += "    "
        else:
            stat = job["stat"]
            c = "r" if stat == "PEND" else "g" if stat == "RUN" else "y"
            l += color(stat.ljust(lens["stat"]), c)
        # user
        if sumjob:
            if len(job["user"]) == 1:
                user = job["user"].keys()[0]
                c = "g" if user == whoami else 0
                username = getuseralias(user)
                l += color(username.ljust(lens["user"]), c)
            else:
                l += color(str(len(job["user"])).ljust(lens["user"]), "b")
        else:
            c = "g" if job["user"] == whoami else 0
            username = getuseralias(job["user"])
            l += color((username + " ").ljust(lens["user"]), c)
        if wide:
            # queue
            if sumjob:
                if len(job["queue"]) == 1:
                    l += job["queue"].keys()[0].ljust(lens["queue"])
                else:
                    nqueues = len(job["queue"])
                    l += color(str(nqueues).ljust(lens["queue"]), "b")
            else:
                l += job["queue"].ljust(lens["queue"])
            # project
            if sumjob:
                if len(job["project"]) == 1:
                    l += job["project"].keys()[0].ljust(lens["project"])
                else:
                    n = len(job["project"])
            else:
                l += job["project"].ljust(lens["project"])
            if not sumjob:
                # priority
                l += str(job["priority"]).rjust(lens["prio."] - 1) + " "
        # wait/runtime
        t = job["run_time"]
        if not sumjob and job["stat"] == "PEND":
                t = time() - job["submit_time"]
        s = format_duration(t)
        l += s.rjust(lens["time"])
        # resources
        # %t
        if job["%complete"]:
            ptime = job["%complete"]
            c = fractioncolor(1 - ptime / 100)
            if wide:
                s = "%6.2f" % round(ptime, 2)
            else:
                s = "%3d" % int(round(ptime))
            l += " " + color(s, c) + "%t"
        elif not sumjob and job["stat"] == "RUN":
            l += "      "
            if wide:
                l += "   "
        # %m
        if job["memlimit"] and job["mem"] and job["slots"]:
            memlimit = job["memlimit"] * job["slots"]
            pmem = 100 * job["mem"] / memlimit
            c = fractioncolor(1 - pmem / 100)
            if wide:
                s = "%6.2f" % round(pmem, 2)
            else:
                s = "%3d" % int(round(pmem))
            l += " " + color(s, c) + "%m"
        elif not sumjob and job["stat"] == "RUN":
            l += "      "
            if wide:
                l += "   "
        # time
        if job["runlimit"]:
            l += "  " + format_duration(job["runlimit"])
        # memory
        if job["memlimit"]:
            l += format_mem(job["memlimit"]).rjust(10)
        else:
            l += "".rjust(10)
        # Hosts
        if job["exec_host"]:
            if wide or len(job["exec_host"]) == 1:
                d = job["exec_host"]
            else:
                d = defaultdict(int)
                for key, val in job["exec_host"].iteritems():
                    d[re.match("(.*?)\d+", key).groups()[0] + "*"] += val
            for key in sorted(d.keys()):
                val = d[key]
                c = "r" if val >= 100 else "y" if val >= 20 else 0
                exclusive = job["exclusive"]
                if sumjob:
                    exclusive = len(exclusive) == 1 and True in exclusive
                times = color("x", "r") if exclusive else "*"
                l += color(" %3d" % val, c) + times + "%s" % key
        else:
            if not sumjob:
                if job["min_req_proc"]:
                    times = color("x", "r") if job["exclusive"] else "*"
                    l += " %3d" % job["min_req_proc"] + times
                elif job["exclusive"]:
                    l += "   1" + color("x", "r")
                else:
                    l += "   1*"
                if job["resreq"]:
                    match = re.search("model==(\w+)", job["resreq"])
                    if match:
                        l += match.groups()[0]
                    if re.search("phi", job["resreq"]):
                        if match:
                            l += "+"
                        l += "Phi"
            if job["rsvd_host"]:
                l += color("  rsvd:", "y")
                if wide or len(job["rsvd_host"]) == 1:
                    d = job["rsvd_host"]
                else:
                    d = defaultdict(int)
                    for key, val in job["rsvd_host"].iteritems():
                        d[re.match("(.*?)\d+", key).groups()[0] + "*"] += val
                for key, val in d.iteritems():
                    c = "r" if val >= 100 else "y" if val >= 20 else 0
                    l += color(" %3d" % val, c) + "*%s" % key
        print(l, file=file)
        file.flush()
Example #2
0
def printjobs(jobs,
              wide=False,
              long=False,
              output=None,
              title=None,
              header=True,
              file=sys.stdout):
    """Print a list of jobs."""
    if len(jobs) == 0:
        return
    sumjob = not isinstance(jobs[0]["jobid"], str)
    if long:
        for job in jobs:
            printjoblong(job, sumjob=sumjob, file=file)
        return
    if output:
        if header:
            print(*output, sep="\t", file=file)
        for job in jobs:
            print(*[job[field] for field in output], sep="\t", file=file)
        return
    # begin output
    whoami = os.getenv("USER")
    namelen = max(map(len, (job["job_name"] for job in jobs)))
    if sumjob:
        titlelen = 0
        if "title" in jobs[0]:
            titlelen = max(map(len, (job["title"] for job in jobs)))
    lens = {
        "title": 10,
        "jobid": 10,
        "name": min(20, max(6, namelen + 1)),
        "stat": 6,
        "user": 10,
        "time": 12,
        "model": 14
    }
    if sumjob:
        lens["stat"] = 12
    else:
        if any(job["jobid"][-1] == "]" for job in jobs):
            lens["jobid"] = 14
    if wide:
        if sumjob:
            lens["title"] = max(6, titlelen + 1)
        lens["name"] = max(6, namelen + 1)
        lens["queue"] = 8
        lens["project"] = 8
        lens["prio."] = 6
    # header
    if header:
        h = ""
        if sumjob and "title" in jobs[0]:
            h += "group".ljust(lens["title"])
        if not sumjob:
            h += "jobid".ljust(lens["jobid"])
        h += "".join(n.ljust(lens[n]) for n in ("name", "stat", "user"))
        if wide:
            h += "".join(n.ljust(lens[n]) for n in ("queue", "project"))
            if not sumjob:
                h += "prio.".ljust(lens["prio."])
        if sumjob:
            h += "runtime".rjust(lens["time"])
        else:
            h += "wait/runtime".rjust(lens["time"])
        h += "  resources"
        h = h.upper()
        if title:
            h += "  " + color(title, "b")
        print(h, file=file)
    for job in jobs:
        l = ""
        if sumjob and "title" in job:
            # title
            title = job["title"]
            if not wide:
                if len(title) >= lens["title"]:
                    title = title[:lens["title"] - 2] + "*"
            l += color(title.ljust(lens["title"]), "b")
        if not sumjob:
            # jobid
            l += (job["jobid"] + " ").ljust(lens["jobid"])
        # job name
        jobname = job["job_name"] if job["job_name"] else ""
        if not wide:
            if len(jobname) >= lens["name"]:
                jobname = "*" + jobname[-lens["name"] + 2:]
        l += jobname.ljust(lens["name"])
        # status
        if sumjob and isinstance(job["stat"], defaultdict):
            l += color("%3d " % job["stat"]["PEND"], "r")
            l += color("%3d " % job["stat"]["RUN"], "g")
            done = job["stat"]["EXIT"] + job["stat"]["DONE"]
            if done:
                l += color("%3d " % done, "y")
            else:
                l += "    "
        else:
            stat = job["stat"]
            if stat == "PEND":
                c = "r"
                if len(job["pend_reason"]) == 1:
                    pr = job["pend_reason"][0]
                    if "New job is waiting for scheduling" in pr[0]:
                        stat = "NEW"
                        c = "b"
                    if "Waiting for rescheduling after parameters" in pr[0]:
                        stat = "MOD"
                        c = "b"
                    if "Job dependency condition not satisfied" in pr[0]:
                        stat = "DEP"
                        c = "b"
            elif stat == "RUN":
                c = "g"
                if job["interactive"]:
                    stat = "INT"
                    if job["X11"]:
                        stat = "X11"
            else:
                c = "y"
            l += color(stat.ljust(lens["stat"]), c)
        # user
        if sumjob and isinstance(job["user"], defaultdict):
            l += color(str(len(job["user"])).ljust(lens["user"]), "b")
        else:
            c = "g" if job["user"] == whoami else 0
            username = getuseralias(job["user"])
            l += color((username + " ").ljust(lens["user"]), c)
        if wide:
            # queue
            if sumjob and isinstance(job["queue"], defaultdict):
                l += color(str(len(job["queue"])).ljust(lens["queue"]), "b")
            else:
                l += job["queue"].ljust(lens["queue"])
            # project
            if sumjob and isinstance(job["project"], defaultdict):
                l += color(
                    str(len(job["project"])).ljust(lens["project"]), "b")
            else:
                l += job["project"].ljust(lens["project"])
            if not sumjob:
                # priority
                l += str(job["priority"]).rjust(lens["prio."] - 1) + " "
        # wait/runtime
        t = job["run_time"]
        if not sumjob and job["stat"] == "PEND":
            t = time() - job["submit_time"]
        s = format_duration(t)
        l += s.rjust(lens["time"])
        # resources
        # %t
        if job["%complete"]:
            ptime = job["%complete"]
            c = fractioncolor(1 - ptime / 100)
            if wide:
                s = "%6.2f" % round(ptime, 2)
            else:
                s = "%3d" % int(round(ptime))
            l += " " + color(s, c) + "%t"
        elif not sumjob and job["stat"] == "RUN":
            l += "      "
            if wide:
                l += "   "
        # %m
        if job["memlimit"] and job["mem"] and job["slots"]:
            memlimit = job["memlimit"] * job["slots"]
            pmem = 100 * job["mem"] / memlimit
            c = fractioncolor(1 - pmem / 100)
            if wide:
                s = "%6.2f" % round(pmem, 2)
            else:
                s = "%3d" % int(round(pmem))
            l += " " + color(s, c) + "%m"
        elif not sumjob and job["stat"] == "RUN":
            l += "      "
            if wide:
                l += "   "
        # time
        if job["runlimit"]:
            l += "  " + format_duration(job["runlimit"])
        # memory
        memlimit = None
        if job["memlimit"]:
            memlimit = job["memlimit"]
            if job["min_req_proc"]:
                memlimit *= job["min_req_proc"]
        if memlimit is not None:
            l += format_mem(memlimit).rjust(10)
        else:
            l += "".rjust(10)
        # Hosts
        if job["exec_host"]:
            if wide or len(job["exec_host"]) == 1:
                d = job["exec_host"]
            else:
                d = defaultdict(int)
                for key, val in job["exec_host"].iteritems():
                    d[re.match("(.*?)\d+", key).groups()[0] + "*"] += val
            for key in sorted(d.keys()):
                val = d[key]
                c = "r" if val >= 100 else "y" if val >= 20 else 0
                exclusive = job["exclusive"]
                if sumjob and exclusive not in (True, False):
                    exclusive = False
                times = color("x", "r") if exclusive else "*"
                l += color(" %3d" % val, c) + times + "%s" % key
        else:
            if not sumjob:
                if job["min_req_proc"]:
                    times = color("x", "r") if job["exclusive"] else "*"
                    l += " %3d" % job["min_req_proc"] + times
                elif job["exclusive"]:
                    l += "   1" + color("x", "r")
                else:
                    l += "   1*"
                if job["host_req"]:
                    hosts = job["host_req"]
                    if len(hosts) == 1:
                        hosts = hosts[0]
                    else:
                        if wide:
                            hosts = "(%s)" % ", ".join(hosts)
                        else:
                            hosts = findstringpattern(hosts)
                    l += hosts.ljust(lens["model"])
                elif job["resreq"]:
                    match = re.search("model==(\w+)", job["resreq"])
                    model = ""
                    if match:
                        model += match.groups()[0]
                    if re.search("phi", job["resreq"]):
                        if match:
                            model += "+"
                        model += "Phi"
                    l += model.ljust(lens["model"])
            if job["alloc_slot"]:
                l += color("  rsvd:", "y")
                if wide or len(job["alloc_slot"]) == 1:
                    d = job["alloc_slot"]
                else:
                    d = defaultdict(int)
                    for key, val in job["alloc_slot"].iteritems():
                        d[re.match("(.*?)\d+", key).groups()[0] + "*"] += val
                for key, val in d.iteritems():
                    c = "r" if val >= 100 else "y" if val >= 20 else 0
                    l += color(" %3d" % val, c) + "*%s" % key
            if wide and job["pend_reason"] and len(job["pend_reason"]) == 1:
                reason = job["pend_reason"][0][0]
                if reason != title:
                    l += color("  %s" % reason, "b")
                    if job["dependency"]:
                        l += color(":", "b")
                if job["dependency"]:
                    l += color(" %s" % job["dependency"], "b")
        print(l, file=file)
        file.flush()
Example #3
0
def printhosts(hosts, jobs=[], wide=False, header=True, file=sys.stdout):
    """list the hosts."""
    if len(hosts) == 0:
        return
    sumhosts = not isinstance(hosts[0]["status"], str)
    jobsbyhost = groupjobs(jobs, "exec_host")
    # begin output
    screencols = int(check_output(["tput", "cols"]))
    whoami = os.getenv("USER")
    namelen = max(map(len, (host["host_name"] for host in hosts)))
    lens = {
        "host_name": min(20, max(6, namelen + 1)),
        "status": 8,
        "title": 15,
        "cpus": 10
    }
    if wide:
        lens["title"] = 20
        lens["host_name"] = max(6, namelen + 1)
        lens["model"] = 14
    if sumhosts:
        lens["status"] = 12
        lens["cpus"] = 14
    if header:
        h = ""
        if sumhosts and "title" in hosts[0]:
            h += "group".ljust(lens["title"])
        h += "".join(n.ljust(lens[n]) for n in ("host_name", "status", "cpus"))
        h += "mem (free/total)"
        if wide:
            h += "  " + "model".ljust(lens["model"])
        h = h.upper()
        print(h, file=file)
    for host in hosts:
        l = ""
        if sumhosts and "title" in host:
            # title
            title = host["title"]
            if not wide:
                if len(title) >= lens["title"]:
                    title = title[:lens["title"] - 2] + "*"
            l += color(title.ljust(lens["title"]), "b")
        # host_name
        l += host["host_name"].ljust(lens["host_name"])
        # status
        if sumhosts:
            l += color("%3d " % host["status"]["ok"], "g")
            closed = sum(n for stat, n in host["status"].iteritems() if
                         stat.startswith("closed_"))
            l += color("%3d " % closed, "r")
            other = len(host["host_names"]) - host["status"]["ok"] - closed
            if other:
                l += color("%3d " % other, "y")
            else:
                l += "    "
        else:
            if host["status"] == "ok":
                l += color("ok".ljust(lens["status"]), "g")
            elif "closed_" in host["status"]:
                l += color(host["status"][7:].ljust(lens["status"]), "r")
            else:
                l += color(host["status"].ljust(lens["status"]), "y")
        # cpus
        total = host["max"]
        used = host["njobs"]
        free = total - used
        c = fractioncolor(free / total)
        if sumhosts:
            l += color("%4d" % free, c) + "/%4d" % total
        else:
            l += color("%2d" % free, c) + "/%2d" % total
        # mem
        if "mem" in host["load"]:
            free, used = host["load"]["mem"]
            total = free + used
            if "maxmem" in host and host["maxmem"]:
                total = host["maxmem"]
            c = fractioncolor(free / total)
            l += "  " + format_mem(free, c) + "/" + format_mem(total)
        if wide:
            if sumhosts:
                if len(host["model"]) == 1:
                    l += host["model"][0].ljust(lens["model"])
                else:
                    nmodel = len(host["model"])
                    l += color(("  %d" % nmodel).ljust(lens["model"]), "b")
            else:
                hoststr = host["model"]
                # Xeon Phi(s)
                phis = 0
                if "mic0" in host["load"]:
                    phis += int(bool(host["load"]["mic0"][0]))
                if "mic1" in host["load"]:
                    phis += int(bool(host["load"]["mic1"][0]))
                if phis > 0:
                    hoststr += "+%dPhi" % phis
                # GPU
                if "gpu" in host["resources"]:
                    hoststr += "+GPU"
                l += "  " + hoststr.ljust(14)
        l += " "
        if host["rsv"] > 0:
            l += " %3d*" % host["rsv"] + color("reserved", "y")
        if sumhosts:
            hostnames = host["host_names"]
        else:
            hostnames = [host["host_name"]]
        jobs = []
        for hostname in hostnames:
            if hostname in jobsbyhost:
                for job in jobsbyhost[hostname]:
                    if job not in jobs:
                        jobs.append(job)
        if sumhosts:
            jobgroups = groupjobs(jobs, "user")
            jobs = []
            for user in sorted(jobgroups.keys()):
                jobs.append(sumjobs(jobgroups[user]))
        if jobs:
            for job in jobs:
                exclusive = job["exclusive"]
                if sumhosts:
                    exclusive = len(exclusive) == 1 and True in exclusive
                times = color("x", "r") if exclusive else "*"
                nslots = sum(job["exec_host"][hn] for hn in hostnames
                             if hn in job["exec_host"])
                c = "r" if nslots >= 100 else "y" if nslots >= 20 else 0
                l += color(" %3d" % nslots, c)
                user = job["user"]
                if sumhosts:
                    user = user.keys()[0]
                c = "g" if user == whoami else 0
                l += times + color(getuseralias(user).ljust(8), c)
                if wide and not sumhosts:
                    if job["mem"]:
                        l += format_mem(job["mem"])
                    else:
                        l += "         "
                    if job["%complete"] and job["runlimit"]:
                        ptime = job["%complete"]
                        c = fractioncolor(1 - ptime / 100)
                        l += color("%3d" % ptime, c) + "% "
                        l += format_duration(job["runlimit"])
        if host["comment"]:
            if sumhosts:
                for key, val in host["comment"].iteritems():
                    if key:
                        l += " %3dx" % val + color(key, "b")
            else:
                l += "   " + color(host["comment"], "b")
        print(l, file=file)
        file.flush()
Example #4
0
def printjobs(jobs, wide=False, long=False, output=None, title=None,
              header=True, file=sys.stdout):
    """Print a list of jobs."""
    if len(jobs) == 0:
        return
    sumjob = not isinstance(jobs[0]["jobid"], str)
    if long:
        for job in jobs:
            printjoblong(job, sumjob=sumjob, file=file)
        return
    if output:
        # header
        print(*output, sep="\t", file=file)
        for job in jobs:
            print(*[job[field] for field in output], sep="\t", file=file)
        return
    # begin output
    whoami = os.getenv("USER")
    namelen = max(map(len, (job["job_name"] for job in jobs)))
    if sumjob:
        titlelen = 0
        if "title" in jobs[0]:
            titlelen = max(map(len, (job["title"] for job in jobs)))
    lens = {
        "title": 10,
        "jobid": 10,
        "name": min(20, max(6, namelen + 1)),
        "stat": 6,
        "user": 10,
        "time": 12,
        "model": 14
    }
    if sumjob:
        lens["stat"] = 12
    else:
        if any(job["jobid"][-1] == "]" for job in jobs):
            lens["jobid"] = 14
    if wide:
        if sumjob:
            lens["title"] = max(6, titlelen + 1)
        lens["name"] = max(6, namelen + 1)
        lens["queue"] = 8
        lens["project"] = 8
        lens["prio."] = 6
    # header
    if header:
        h = ""
        if sumjob and "title" in jobs[0]:
            h += "group".ljust(lens["title"])
        if not sumjob:
            h += "jobid".ljust(lens["jobid"])
        h += "".join(n.ljust(lens[n]) for n in ("name", "stat", "user"))
        if wide:
            h += "".join(n.ljust(lens[n]) for n in ("queue", "project"))
            if not sumjob:
                h += "prio.".ljust(lens["prio."])
        if sumjob:
            h += "runtime".rjust(lens["time"])
        else:
            h += "wait/runtime".rjust(lens["time"])
        h += "  resources"
        h = h.upper()
        if title:
            h += "  " + color(title, "b")
        print(h, file=file)
    for job in jobs:
        l = ""
        if sumjob and "title" in job:
            # title
            title = job["title"]
            if not wide:
                if len(title) >= lens["title"]:
                    title = title[:lens["title"] - 2] + "*"
            l += color(title.ljust(lens["title"]), "b")
        if not sumjob:
            # jobid
            l += (job["jobid"] + " ").ljust(lens["jobid"])
        # job name
        jobname = job["job_name"] if job["job_name"] else ""
        if not wide:
            if len(jobname) >= lens["name"]:
                jobname = "*" + jobname[-lens["name"] + 2:]
        l += jobname.ljust(lens["name"])
        # status
        if sumjob and isinstance(job["stat"], defaultdict):
            l += color("%3d " % job["stat"]["PEND"], "r")
            l += color("%3d " % job["stat"]["RUN"], "g")
            done = job["stat"]["EXIT"] + job["stat"]["DONE"]
            if done:
                l += color("%3d " % done, "y")
            else:
                l += "    "
        else:
            stat = job["stat"]
            if stat == "PEND":
                c = "r"
                if len(job["pend_reason"]) == 1:
                    pr = job["pend_reason"][0]
                    if "New job is waiting for scheduling" in pr[0]:
                        stat = "NEW"
                        c = "b"
                    if "Waiting for rescheduling after parameters" in pr[0]:
                        stat = "MOD"
                        c = "b"
                    if "Job dependency condition not satisfied" in pr[0]:
                        stat = "DEP"
                        c = "b"
            elif stat == "RUN":
                c = "g"
                if job["interactive"]:
                    stat = "INT"
                    if job["X11"]:
                        stat = "X11"
            else:
                c = "y"
            l += color(stat.ljust(lens["stat"]), c)
        # user
        if sumjob and isinstance(job["user"], defaultdict):
            l += color(str(len(job["user"])).ljust(lens["user"]), "b")
        else:
            c = "g" if job["user"] == whoami else 0
            username = getuseralias(job["user"])
            l += color((username + " ").ljust(lens["user"]), c)
        if wide:
            # queue
            if sumjob and isinstance(job["queue"], defaultdict):
                l += color(str(len(job["queue"])).ljust(lens["queue"]), "b")
            else:
                l += job["queue"].ljust(lens["queue"])
            # project
            if sumjob and isinstance(job["project"], defaultdict):
                l += color(str(len(job["project"])).ljust(lens["project"]),
                           "b")
            else:
                l += job["project"].ljust(lens["project"])
            if not sumjob:
                # priority
                l += str(job["priority"]).rjust(lens["prio."] - 1) + " "
        # wait/runtime
        t = job["run_time"]
        if not sumjob and job["stat"] == "PEND":
                t = time() - job["submit_time"]
        s = format_duration(t)
        l += s.rjust(lens["time"])
        # resources
        # %t
        if job["%complete"]:
            ptime = job["%complete"]
            c = fractioncolor(1 - ptime / 100)
            if wide:
                s = "%6.2f" % round(ptime, 2)
            else:
                s = "%3d" % int(round(ptime))
            l += " " + color(s, c) + "%t"
        elif not sumjob and job["stat"] == "RUN":
            l += "      "
            if wide:
                l += "   "
        # %m
        if job["memlimit"] and job["mem"] and job["slots"]:
            memlimit = job["memlimit"] * job["slots"]
            pmem = 100 * job["mem"] / memlimit
            c = fractioncolor(1 - pmem / 100)
            if wide:
                s = "%6.2f" % round(pmem, 2)
            else:
                s = "%3d" % int(round(pmem))
            l += " " + color(s, c) + "%m"
        elif not sumjob and job["stat"] == "RUN":
            l += "      "
            if wide:
                l += "   "
        # time
        if job["runlimit"]:
            l += "  " + format_duration(job["runlimit"])
        # memory
        memlimit = None
        if job["memlimit"]:
            memlimit = job["memlimit"]
            if job["min_req_proc"]:
                memlimit *= job["min_req_proc"]
        if memlimit is not None:
            l += format_mem(memlimit).rjust(10)
        else:
            l += "".rjust(10)
        # Hosts
        if job["exec_host"]:
            if wide or len(job["exec_host"]) == 1:
                d = job["exec_host"]
            else:
                d = defaultdict(int)
                for key, val in job["exec_host"].iteritems():
                    d[re.match("(.*?)\d+", key).groups()[0] + "*"] += val
            for key in sorted(d.keys()):
                val = d[key]
                c = "r" if val >= 100 else "y" if val >= 20 else 0
                exclusive = job["exclusive"]
                if sumjob and exclusive not in (True, False):
                    exclusive = False
                times = color("x", "r") if exclusive else "*"
                l += color(" %3d" % val, c) + times + "%s" % key
        else:
            if not sumjob:
                if job["min_req_proc"]:
                    times = color("x", "r") if job["exclusive"] else "*"
                    l += " %3d" % job["min_req_proc"] + times
                elif job["exclusive"]:
                    l += "   1" + color("x", "r")
                else:
                    l += "   1*"
                if job["host_req"]:
                    hosts = job["host_req"]
                    if len(hosts) == 1:
                        hosts = hosts[0]
                    else:
                        if wide:
                            hosts = "(%s)" % ", ".join(hosts)
                        else:
                            hosts = findstringpattern(hosts)
                    l += hosts.ljust(lens["model"])
                elif job["resreq"]:
                    match = re.search("model==(\w+)", job["resreq"])
                    model = ""
                    if match:
                        model += match.groups()[0]
                    if re.search("phi", job["resreq"]):
                        if match:
                            model += "+"
                        model += "Phi"
                    l += model.ljust(lens["model"])
            if job["alloc_slot"]:
                l += color("  rsvd:", "y")
                if wide or len(job["alloc_slot"]) == 1:
                    d = job["alloc_slot"]
                else:
                    d = defaultdict(int)
                    for key, val in job["alloc_slot"].iteritems():
                        d[re.match("(.*?)\d+", key).groups()[0] + "*"] += val
                for key, val in d.iteritems():
                    c = "r" if val >= 100 else "y" if val >= 20 else 0
                    l += color(" %3d" % val, c) + "*%s" % key
            if wide and job["pend_reason"] and len(job["pend_reason"]) == 1:
                reason = job["pend_reason"][0][0]
                if reason != title:
                    l += color("  %s" % reason, "b")
                    if job["dependency"]:
                        l += color(":", "b")
            if job["dependency"]:
                l += color(" %s" % job["dependency"], "b")
        print(l, file=file)
        file.flush()
Example #5
0
def printhosts(hosts, jobs=[], wide=False, header=True, file=sys.stdout):
    """Print a list of hosts."""
    if len(hosts) == 0:
        return
    sumhosts = not isinstance(hosts[0]["status"], str)
    jobsbyhost = groupjobs(jobs, "exec_host")
    # begin output
    screencols = int(check_output(["tput", "cols"]))
    whoami = os.getenv("USER")
    namelen = max(map(len, (host["host_name"] for host in hosts)))
    lens = {
        "host_name": min(20, max(6, namelen + 1)),
        "status": 8,
        "title": 15,
        "cpus": 10
    }
    if wide:
        lens["title"] = 20
        lens["host_name"] = max(6, namelen + 1)
        lens["model"] = 14
    if sumhosts:
        lens["status"] = 12
        lens["cpus"] = 14
    if header:
        h = ""
        if sumhosts and "title" in hosts[0]:
            h += "group".ljust(lens["title"])
        h += "".join(n.ljust(lens[n]) for n in ("host_name", "status", "cpus"))
        h += "mem (free/total)"
        if wide:
            h += "  " + "model".ljust(lens["model"])
        h = h.upper()
        print(h, file=file)
    for host in hosts:
        l = ""
        if sumhosts and "title" in host:
            # title
            title = host["title"]
            if not wide:
                if len(title) >= lens["title"]:
                    title = title[:lens["title"] - 2] + "*"
            l += color(title.ljust(lens["title"]), "b")
        # host_name
        l += host["host_name"].ljust(lens["host_name"])
        # status
        if sumhosts:
            l += color("%3d " % host["status"]["ok"], "g")
            closed = sum(n for stat, n in host["status"].iteritems()
                         if stat.startswith("closed_"))
            l += color("%3d " % closed, "r")
            other = len(host["host_names"]) - host["status"]["ok"] - closed
            if other:
                l += color("%3d " % other, "y")
            else:
                l += "    "
        else:
            if host["status"] == "ok":
                l += color("ok".ljust(lens["status"]), "g")
            elif "closed_" in host["status"]:
                l += color(host["status"][7:].ljust(lens["status"]), "r")
            else:
                l += color(host["status"].ljust(lens["status"]), "y")
        # cpus
        total = host["max"]
        used = host["njobs"]
        free = total - used
        c = fractioncolor(free, total)
        if sumhosts:
            l += color("%4d" % free, c) + "/%4d" % total
        else:
            l += color("%2d" % free, c) + "/%2d" % total
        # mem
        if "mem" in host["load"]:
            free, used = host["load"]["mem"]
            total = free
            if used:  # used can be None
                total += used
            if "maxmem" in host and host["maxmem"]:
                total = host["maxmem"]
            c = fractioncolor(free, total)
            l += "  " + format_mem(free, c) + "/" + format_mem(total)
        if wide:
            if sumhosts:
                if len(host["model"]) == 1:
                    l += host["model"][0].ljust(lens["model"])
                else:
                    nmodel = len(host["model"])
                    l += color(("  %d" % nmodel).ljust(lens["model"]), "b")
            else:
                hoststr = host["model"]
                # Xeon Phi(s)
                phis = 0
                if "mic0" in host["load"]:
                    phis += int(bool(host["load"]["mic0"][0]))
                    phis += int(bool(host["load"]["mic0"][1]))
                if "mic1" in host["load"]:
                    phis += int(bool(host["load"]["mic1"][0]))
                    phis += int(bool(host["load"]["mic1"][1]))
                if phis > 0:
                    hoststr += "+%dPhi" % phis
                # GPU
                if "gpu" in host["resources"]:
                    hoststr += "+GPU"
                l += "  " + hoststr.ljust(14)
        l += " "
        if host["rsv"] > 0:
            l += " %3d*" % host["rsv"] + color("reserved", "y")
        if sumhosts:
            hostnames = host["host_names"]
        else:
            hostnames = [host["host_name"]]
        jobs = []
        for hostname in hostnames:
            if hostname in jobsbyhost:
                for job in jobsbyhost[hostname]:
                    if job not in jobs:
                        jobs.append(job)
        if sumhosts:
            jobgroups = groupjobs(jobs, "user")
            jobs = []
            for user in sorted(jobgroups.keys()):
                jobs.append(sumjobs(jobgroups[user]))
        if jobs:
            for job in jobs:
                exclusive = job["exclusive"]
                if sumhosts:
                    exclusive = len(exclusive) == 1 and True in exclusive
                times = color("x", "r") if exclusive else "*"
                nslots = sum(job["exec_host"][hn] for hn in hostnames
                             if hn in job["exec_host"])
                c = "r" if nslots >= 100 else "y" if nslots >= 20 else 0
                l += color(" %3d" % nslots, c)
                user = job["user"]
                if sumhosts:
                    user = user.keys()[0]
                c = "g" if user == whoami else 0
                l += times + color(getuseralias(user).ljust(8), c)
                if wide and not sumhosts:
                    if job["mem"]:
                        l += format_mem(job["mem"])
                    else:
                        l += "         "
                    if job["%complete"] and job["runlimit"]:
                        ptime = job["%complete"]
                        c = fractioncolor(1 - ptime / 100)
                        l += color("%3d" % ptime, c) + "% "
                        l += format_duration(job["runlimit"])
        if host["comment"]:
            if sumhosts:
                for key, val in host["comment"].iteritems():
                    if key:
                        l += " %3dx" % val + color(key, "b")
            else:
                l += "   " + color(host["comment"], "b")
        print(l, file=file)
        file.flush()