This scripts strips all comments in the input JSON file, modifies the parameters according to commnad line arguments, and write the content to a new JSON file.
Signed-off-by: Pi-Cheng Chen pi-cheng.chen@linaro.org --- doc/tune_json.py | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100755 doc/tune_json.py
diff --git a/doc/tune_json.py b/doc/tune_json.py new file mode 100755 index 0000000..36746ff --- /dev/null +++ b/doc/tune_json.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python + +import collections +import argparse +import shutil +import os +import sys +import json +import re + + +def find_dict_by_key(doc, key): + if key in doc and type(doc[key]) is collections.OrderedDict: + return doc[key] + + for k in doc: + if type(doc[k]) is collections.OrderedDict: + return find_dict_by_key(doc[k], key) + + +def dict_find_and_replace_value(dic, key, val): + for k in dic: + if type(dic[k]) is collections.OrderedDict: + dict_find_and_replace_value(dic[k], key, val) + if k == key: + dic[k] = val + + +def dict_of_loading(dic): + if not 'run' in dic: + return False, None + + for k in dic: + if 'timer' in k and 'period' in dic[k]: + return True, k + else: + return False, None + + +def calculate_and_update_loading(dic, loading): + of_loading, timer_id = dict_of_loading(dic) + + if of_loading: + period = dic[timer_id]['period'] + run = period * loading / 100 + dic['run'] = run + + for k in dic: + if type(dic[k]) is collections.OrderedDict: + calculate_and_update_loading(dic[k], loading) + + +# strip comments in json file and load the file as a dict +def load_json_file(filename): + try: + f = open(filename, 'r') + except: + print 'ERROR: Unable to open %s' %filename + sys.exit(2) + + comment_re = re.compile( + '(^)?[^\S\n]*/(?:*(.*?)*/[^\S\n]*|/[^\n]*)($)?', + re.DOTALL | re.MULTILINE) + + content = ''.join(f.readlines()) + + match = comment_re.search(content) + while match: + content = content[:match.start()] + content[match.end():] + match = comment_re.search(content) + + return json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode(content) + + +def dump_json_file(doc, filename): + try: + fo = open(filename, 'w+') + except: + print 'ERROR: Unable to open %s' %filename + sys.exit(2) + + json.dump(doc, fo, indent=4, sort_keys=False) + fo.close() + + +if __name__ == '__main__': + tmp = 'tmp.json' + + parser = argparse.ArgumentParser() + + parser.add_argument('-f', '--file', dest='infile', default='', help='input json filename') + parser.add_argument('-o', '--out', dest='outfile', default='workload.json', help='output json filename'); + parser.add_argument('--instance', default=0, type=int, help='number of thread instance') + parser.add_argument('--period', default=0, type=int, help='period of each thread/phase (ms)') + parser.add_argument('--run', default=0, type=int, help='run time of each thread/phase (ms)') + parser.add_argument('--sleep', default=0, type=int, help='sleep time of each thread/phase (ms)') + parser.add_argument('--loop', default=0,type=int, help='loop count of each thread/phase (-1 as infinite loop)') + parser.add_argument('--loading', default=0, type=int, help='loading of each thread (%%)') + parser.add_argument('--key', type=str, help='the key id of thread/phase in which the parameters will be changed') + + args = parser.parse_args() + + if not os.path.isfile(args.infile): + print 'ERROR: input file %s does not exist\n' %args.infile + parser.print_help() + sys.exit(2) + + shutil.copyfile(args.infile, tmp) + + doc = target = load_json_file(tmp) + + if args.key: + target = find_dict_by_key(doc, args.key) + if not target: + print 'ERROR: key id %s is not found' %args.key + sys.exit(2) + + if args.instance > 0: + dict_find_and_replace_value(target, 'instance', args.instance) + + if args.period > 0: + dict_find_and_replace_value(target, 'period', args.period * 1000) + + if args.run > 0: + dict_find_and_replace_value(target, 'run', args.run * 1000) + + if args.sleep > 0: + dict_find_and_replace_value(target, 'sleep', args.sleep * 1000) + + if args.loop > 0 or args.loop == -1: + dict_find_and_replace_value(target, 'loop', args.loop) + + if args.loading > 0: + calculate_and_update_loading(target, args.loading); + + dump_json_file(doc, tmp) + + os.rename(tmp, args.outfile)
A JSON file to describe a workload with several 10% loading small tasks.
Signed-off-by: Pi-Cheng Chen pi-cheng.chen@linaro.org --- doc/examples/example6.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 doc/examples/example6.json
diff --git a/doc/examples/example6.json b/doc/examples/example6.json new file mode 100644 index 0000000..97ff02e --- /dev/null +++ b/doc/examples/example6.json @@ -0,0 +1,14 @@ +{ + /* + * Simple use case which creates 10% load small tasks that run + * until the use case is stopped with Ctrl+C + */ + "tasks" : { + "thread0" : { + "instance" : 4, + "loop" : -1, + "run" : 1000, + "timer" : { "ref" : "tick", "period" : 10000 } + } + } +}
On 12 November 2014 02:49, pi-cheng.chen pi-cheng.chen@linaro.org wrote:
A JSON file to describe a workload with several 10% loading small tasks.
Hi Pi-Cheng,
Your example6.json is quite similar to example 2. Please add an "instance" : 1, in example2.json instead of creating a new example
Regards, Vincent
Signed-off-by: Pi-Cheng Chen pi-cheng.chen@linaro.org
doc/examples/example6.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 doc/examples/example6.json
diff --git a/doc/examples/example6.json b/doc/examples/example6.json new file mode 100644 index 0000000..97ff02e --- /dev/null +++ b/doc/examples/example6.json @@ -0,0 +1,14 @@ +{
/*
* Simple use case which creates 10% load small tasks that run
* until the use case is stopped with Ctrl+C
*/
"tasks" : {
"thread0" : {
"instance" : 4,
"loop" : -1,
"run" : 1000,
"timer" : { "ref" : "tick", "period" : 10000 }
}
}
+}
1.9.1
On 19 November 2014 16:32, Vincent Guittot vincent.guittot@linaro.org wrote:
On 12 November 2014 02:49, pi-cheng.chen pi-cheng.chen@linaro.org wrote:
A JSON file to describe a workload with several 10% loading small tasks.
Hi Pi-Cheng,
Your example6.json is quite similar to example 2. Please add an "instance" : 1, in example2.json instead of creating a new example
Regards, Vincent
Hi Vincent,
Ok. I'll do it. Thanks for reviewing.
Best Regards, Pi-Cheng
Fix all trailing commas in /doc/examples/* so that those JSON file could be tuned by tune_json.py script. A trailing comma in mp3.json needs to be removed manually since it's added by workgen script when adding suspend key.
Signed-off-by: Pi-Cheng Chen pi-cheng.chen@linaro.org --- doc/examples/browser.json | 36 ++++++++++++++++++------------------ doc/examples/example1.json | 6 +++--- doc/examples/example2.json | 6 +++--- doc/examples/example3.json | 8 ++++---- doc/examples/example4.json | 8 ++++---- doc/examples/mp3.json | 16 ++++++++-------- 6 files changed, 40 insertions(+), 40 deletions(-)
diff --git a/doc/examples/browser.json b/doc/examples/browser.json index a5b4dee..b1436c6 100644 --- a/doc/examples/browser.json +++ b/doc/examples/browser.json @@ -9,57 +9,57 @@ "run" : 15000, "resume" : "Browser", "run" : 7000, - "sleep" : 8000, + "sleep" : 8000 }, "render1" : { "loop" : 50, "resume" : "BrowserSub", - "run" : 3000, + "run" : 3000 }, "render2" : { "loop" : 1, "suspend" : "Browser", "run" : 10000, "resume" : "Browser", - "run" : 5000, + "run" : 5000 }, "render3" : { "loop" : 20, "resume" : "BrowserSub", - "run" : 3000, + "run" : 3000 }, "stop" : { "loop" : 1, "run" : 2000, "sleep" : 200000, "suspend" : "Browser", - "sleep" : 600000, + "sleep" : 600000 }, "scroll" : { "loop" : 4, "resume" : "Browser", "suspend" : "BrowserNext", - "run" : 1000, + "run" : 1000 }, "stop2" : { "loop" : 1, "suspend" : "Browser", "run" : 200, - "sleep" : 800000, - }, + "sleep" : 800000 + } } }, "BrowserSub1" : { "priority" : -6, "loop" : -1, "suspend" : "BrowserSub", - "run" : 100, + "run" : 100 }, "BrowserSub2" : { "priority" : -6, "loop" : -1, "suspend" : "BrowserSub", - "run" : 100, + "run" : 100 }, "BrowserDisplay" : { "priority" : -6, @@ -73,7 +73,7 @@ "unlock" : "mutex11", "run" : 300, "resume" : "Binder-display", - "run" : 400, + "run" : 400 }, "Binder-dummy" : { "priority" : -6, @@ -85,7 +85,7 @@ "lock" : "mutex11", "signal" : "queue11", "unlock" : "mutex11", - "run" : 100, + "run" : 100 }, "Binder-display" : { "priority" : -6, @@ -93,7 +93,7 @@ "suspend", "run" : 300, "resume" : "Event-Browser", - "resume" : "Event-Display", + "resume" : "Event-Display" }, "Event-Browser" : { "priority" : -9, @@ -102,7 +102,7 @@ "run" : 50, "sleep" : 16000, "run" : 50, - "resume" : "Browser", + "resume" : "Browser" }, "Event-Display" : { "priority" : -9, @@ -111,13 +111,13 @@ "run" : 50, "sleep" : 16000, "run" : 50, - "resume" : "Display", + "resume" : "Display" }, "Display" : { "priority" : -8, "loop" : -1, "suspend", - "run" : 16000, - }, - }, + "run" : 16000 + } + } } diff --git a/doc/examples/example1.json b/doc/examples/example1.json index bf3fe1e..df4df4f 100644 --- a/doc/examples/example1.json +++ b/doc/examples/example1.json @@ -7,7 +7,7 @@ "thread0" : { "loop" : -1, "run" : 1000, - "sleep" : 9000, - }, - }, + "sleep" : 9000 + } + } } diff --git a/doc/examples/example2.json b/doc/examples/example2.json index c5116f6..f43d825 100644 --- a/doc/examples/example2.json +++ b/doc/examples/example2.json @@ -7,7 +7,7 @@ "thread0" : { "loop" : -1, "run" : 1000, - "timer" : { "ref" : "tick", "period" : 10000 }, - }, - }, + "timer" : { "ref" : "tick", "period" : 10000 } + } + } } diff --git a/doc/examples/example3.json b/doc/examples/example3.json index 56039c3..4ec6e0d 100644 --- a/doc/examples/example3.json +++ b/doc/examples/example3.json @@ -10,14 +10,14 @@ "light" : { "loop" : 100, "run" : 1000, - "timer" : { "ref" : "tick", "period" : 10000 }, + "timer" : { "ref" : "tick", "period" : 10000 } }, "heavy" : { "loop" : 100, "run" : 9000, - "timer" : { "ref" : "tick", "period" : 10000 }, + "timer" : { "ref" : "tick", "period" : 10000 } } } - }, - }, + } + } } diff --git a/doc/examples/example4.json b/doc/examples/example4.json index 2865075..19d2be2 100644 --- a/doc/examples/example4.json +++ b/doc/examples/example4.json @@ -8,13 +8,13 @@ "loop" : -1, "run" : 10000, "resume" : "thread1", - "suspend", + "suspend" }, "thread1" : { "loop" : -1, "run" : 10000, "resume" : "thread0", - "suspend", - }, - }, + "suspend" + } + } } diff --git a/doc/examples/mp3.json b/doc/examples/mp3.json index c1b40f0..b42e7d5 100644 --- a/doc/examples/mp3.json +++ b/doc/examples/mp3.json @@ -7,11 +7,11 @@ "p1" : { "loop" : 1, "resume" : "AudioOut", - "timer" : { "ref" : "tick", "period": 6000 }, + "timer" : { "ref" : "tick", "period": 6000 } }, "p2" : { "loop" : 4, - "timer" : { "ref" : "tick", "period": 6000 }, + "timer" : { "ref" : "tick", "period": 6000 } } } }, @@ -21,14 +21,14 @@ "run" : 275, "resume" : "AudioTrack", "run" : 4725, - "suspend", + "suspend" }, "AudioTrack" : { "priority" : -16, "loop" : -1, "suspend", "run" : 300, - "resume" : "mp3.decoder", + "resume" : "mp3.decoder" }, "mp3.decoder" : { "priority" : -2, @@ -39,7 +39,7 @@ "signal" : "queue", "wait" : { "ref" : "queue", "mutex": "mutex" }, "unlock" : "mutex", - "run" : 150, + "run" : 150 }, "OMXCall" : { "priority" : -2, @@ -50,8 +50,8 @@ "run" : 300, "lock" : "mutex", "signal" : "queue", - "unlock" : "mutex", - }, + "unlock" : "mutex" + } }, "global" : { "default_policy" : "SCHED_OTHER", @@ -62,6 +62,6 @@ "log_basename" : "mp3", "lock_pages" : true, "frag" : 1, - "calibration" : "CPU0", + "calibration" : "CPU0" } }