Worst case code time analysis - Page 1
Forum section: electronics / beginners

author: A2
author_rank: Contributor
timestamp: July 29, 2014, 04:18:42 pm
content: I have written a code:Code:while(1){sample_data();check_48_cases_for_sampled_data();}I have sample data. then according to sampled data I have 48 cases. Depending upon data all or different cases may execute. Individual case time tme again may vary according to data value passed.So how to calculate worst case I mean max time taken by loop.1. This is very general example I took. I want to know is there any software/tool available which can do this analysis like pass sample data & tell me that for these values code will crash & these values max time will be taken

author: bwat
author_rank: Frequent Contributor
timestamp: July 29, 2014, 05:10:21 pm
content: It all depends on the computer. You'll have to model things like cache misses, virtual memory TLB misses, pipeline stalls, etc. I've written tools that decompiled a binary into basic blocks then tried to anotate the graph with worst case execution times. It's not as easy as it first seems. The easiest thing to do is actually to design for such analyses, i.e. choose a processor with deterministic execution times and write your code with upper bounds on all loops.

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 29, 2014, 05:59:33 pm
content: Quote from: A2 on July 29, 2014, 04:18:42 pmyou want worst case execution time, and then want a software for crash/stability analysis? thats 2 entirely different things! please rephrase what you want... assuming you want worst time case because it is phrased twice in your post.... no need software, just calculate by hand how many opcodes (so to speak) each "branch" (remember i say each branch not each case. one case may have many branches if/case in C keyword term), calculate total or cumulative opcodes count for any possible branch, sort it and you have max opcodes hence max time scenario. and then retrace what value that can cause that branch to execute. you expect a software that will feed the routine from 1 to 4294967296 in an optimized amount of time? good luck! assuming you are in luck, enjoy the time debugging!

author: Rick Law
author_rank: Super Contributor
timestamp: July 30, 2014, 04:27:16 am
content: Since you wrote your question in high-level language, I assume you may not know this:Your C compiler will convert high level language to machine codes (aka op codes + data). For example, a statement such as "count=8+value" may translate to:movax,value ; get the content of memory containing variable value into register ax; note that this may be more than 1 CPU word (8bit, 16bit, 32bit... whatver);movax is the first word (some cpu may have 2 word); this first word is your op-code; following it is the second (or second and third) word containing the; address for variable callvalue-- ie: the data part of the machine codesmovbx,8 ; set register bx to 8addax,bx ; add bx to ax and results in axmovcount,ax ; save ax to memory address call countEach of these op codes (mov, add) takes a certain number of CPU cycles.movax,value ; this may take longer because it moves from memorymovbx,8 ; this may take shorter because it is an embedded fix value in the codeEach CPU has it own designed instruction set, and each instruction takes a certain number of cycles. The example above is the 8080 instruction set. Some compilers spills out assembler as an intermediate step which is then converted to machine code by a Macro Assembler.Now you know this, you can do asMechatrommersuggested: you look at the compiled object code (machine codes - to be loaded to your MCU/CPU), match it to the corresponding instruction according to the CPU op codes so you end up with something like the above. Now add the total clock cycles you counted for each possible path.Since you asked for worst case, cache miss or other issues like decode stack dump with a branch doesn't matter. Cache and pre-decoding are two of the ways to speed things up. You are looking for worst case.After you get those, you should also considerinterrupt. When aninterruptoccurs such as char arrival at the port with a serial Rx, your CPU is dragged away to do something else. That would mess up your carefully counted cycles since your CPU for X number of cycles is doing something else. If number clock cycles is important, you should consider how aninterruptmay mess things up for you.Hope this helpsRick

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 30, 2014, 12:31:35 pm
content: now to get things much easier (instead of counting opcodes), we dont usually care codes that run in O(1), we usually struggle to find code of O(n) and if possible in anyway try to avoid O(n*n) complexity in any branch whatsoever. whenever you start worrying O(1) codes then its either you dont have enough CPU resources (or wrong selection thereis) or have to do the above mentioned detailed method. recursive code and memory allocation is another thing, the answer will depend on your selected hardware. no one can give you the absolute right answer. for the simple hint i think of this moment, "take care whats going on in a loop anywhere", "avoid recursive code", and avoid "memory intensive code" if you really in tight arse, selecting between "memory vs cpu" intensive code is another art. ymmv.

author: bwat
author_rank: Frequent Contributor
timestamp: July 30, 2014, 01:05:20 pm
content: Quote from: Mechatrommer on July 30, 2014, 12:31:35 pmI think your description there is very very sketchy. Thebig-O notationis just a measure of the asymptotic efficiency of algorithms but you're using it for timing analysis. The big-O notation is used to identify a set of functions that share an asymptotic upper bound. It's not a timing measurement.Also the accuracy of calculation of worst case execution time is often very important, e.g. in the case of feasibility tests with regards to hard real-time scheduling algorithms. See the first chapter, "Step 1", in the following for examples of such tests for the Rate Monotonic, Deadline Monotonic and Earliest Deadline First algorithms:http://barrywatson.se/download/Everyman.pdf. You just don't guess this stuff.

author: dfmischler
author_rank: Frequent Contributor
timestamp: July 30, 2014, 01:30:18 pm
content: Automate and measure. Turn off all caching (if any), then measure how long the code takes to run with all of the samples set to each possible value. The sample value that takes longest gives you the worst-case path through the code with all cache misses. If necessary, re-run your measurements with the worst case path, maximum interrupt rate, and/or DMA transfers. You'll come pretty close. Note that you can get pretty complex behavior (due to short term priority inversions) out of a RTOS or multi-tasking environment.

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 31, 2014, 01:41:53 am
content: Quote from: bwat on July 30, 2014, 01:05:20 pmyou'll find in any academic writing using this "asymptotic time bound" analysis. my "sketchy" assumption doesnt went too far, any constant number is O(1) 3x2+4x+2=O(x2) change the multiplers and its still O(x2). and this asymptotic analysis is highly correlated to time analysis result, thats the "physics" that cannot be bent (so far). as i said to get "things much easier" "machine independent" result. whereas time analysis you are refering to is maybe the precise micro seconds result? or some "insert famous name" index. that is machine dependent and you'll get different figure per machine its running (or even wheather). since OP is asking generalized question, i'm answering one.ymmv hope that help.

author: miguelvp
author_rank: Super Contributor
timestamp: July 31, 2014, 01:44:43 am
content: google code profiler or code profiling.http://en.wikipedia.org/wiki/Profiling_(computer_programming)Not sure what architecture you are talking about, for x86 family VTune by Intel sure is the more feature rich profiler, but there are others like AMD CodeAnalyst (less features but probably a bit more accurate?)BTW VTune is also available for Linux not just Windows