def main(stdscr_p): global stdscr stdscr = _StandardOutput(stdscr_p) sort = "" duration = 0.0 i = 1 ss = Subsystem() ss.init_processor_metrics() ss.init_memory_metrics() ss.init_disk_metrics() ss.init_network_metrics() ss.init_process_metrics() cpu = _ProcessorPrint(ss, stdscr) mem = _MemoryPrint(ss, stdscr) disk = _DiskPrint(ss, stdscr) net = _NetPrint(ss, stdscr) proc = _ProcPrint(ss, stdscr) proc.output_type = opts.output_type stdscr.width = opts.width pmc = pmapi.pmContext.fromOptions(opts.opts, sys.argv) if pmc.type == c_api.PM_CONTEXT_ARCHIVE: pmc.pmSetMode(c_api.PM_MODE_FORW, pmapi.timeval(0, 0), 0) host = pmc.pmGetContextHostName() (delta, errmsg) = pmc.pmParseInterval(str(opts.interval_arg) + " seconds") ss.setup_metrics(pmc) if opts.create_archive: delta_seconds = c_api.pmtimevalToReal(delta.tv_sec, delta.tv_usec) msec = str(int(1000.0 * delta_seconds)) configuration = "log mandatory on every " + msec + " milliseconds { " configuration += ss.dump_metrics() configuration += "}" if opts.n_samples != 0: duration = float(opts.n_samples) * delta_seconds else: duration = float(10) * delta_seconds status = record(pmgui.GuiClient(), configuration, duration, opts.output_file, host) if status != "": return status record_add_creator(opts.output_file) sys.exit(0) i_samples = 0 disk.interval = delta.tv_sec disk.replay_archive = opts.replay_archive try: elapsed = ss.get_metric_value('kernel.all.uptime') while (i_samples < opts.n_samples) or (opts.n_samples == 0): ss.get_stats(pmc) stdscr.move(0, 0) stdscr.addstr('ATOP - %s %s elapsed\n\n' % ( time.strftime("%c"), datetime.timedelta(0, elapsed))) elapsed = delta.tv_sec stdscr.move(2, 0) try: cpu.prc() cpu.cpu() mem.mem() disk.disk(pmc) net.net(pmc) proc.set_line() proc.proc() except pmapi.pmErr as e: return str(e) + " while processing " + str(ssx[0]) except Exception as e: # catch all errors, pcp or python or other pass stdscr.move(proc.command_line, 0) stdscr.refresh() stdscr.timeout(delta.tv_sec * 1000) char = stdscr.getch() if char != -1: # user typed a command try: cmd = chr(char) except ValueError: cmd = None if cmd == "q": raise KeyboardInterrupt elif cmd == "":
if opts.duration_arg != 0: (timeval, errmsg) = pm.pmParseInterval(str(opts.duration_arg)) duration = c_api.pmtimevalToReal(timeval.tv_sec, timeval.tv_usec) opts.n_samples = int((duration / delta_seconds) + 0.5) if opts.create_archive: msec = str(int(1000.0 * delta_seconds)) configuration = "log mandatory on every " + msec + " milliseconds {\n" configuration += ss.dump_metrics().replace(" ", "\n") configuration += "}\n" if duration == 0.0: if opts.n_samples != 0: duration = float(opts.n_samples) * delta_seconds else: duration = float(10) * delta_seconds record(pmgui.GuiClient(), configuration, duration, opts.output_file, host) record_add_creator(opts.output_file) sys.exit(0) try: ss.setup_metrics(pm) ss.get_stats(pm) except pmapi.pmErr as e: if replay_archive: import textwrap print("One of the following metrics is required " + \ "but absent in " + input_file + "\n" + \ textwrap.fill(str(ss.metrics))) else: print("unable to setup metrics")
return errmsg duration = timeval.tv_sec ss.setup_metrics (pmc) if create_archive: configuration = "log mandatory on every " + \ str(interval_arg) + " seconds { " configuration += ss.dump_metrics() configuration += "}" if duration == 0: if n_samples != 0: duration = n_samples * interval_arg else: duration = 10 * interval_arg status = record (host, pmgui.GuiClient(), configuration, duration, output_file) if status != "": return status record_add_creator (output_file) sys.exit(0) i_samples = 0 subsys_cmds = ['g', 'm'] (delta, errmsg) = pmc.pmParseInterval(str(interval_arg) + " seconds") disk.interval = delta.tv_sec disk.replay_archive = replay_archive try: elapsed = ss.get_metric_value('kernel.all.uptime') while (i_samples < n_samples) or (n_samples == 0):