anbarasans85 has asked for the wisdom of the Perl Monks concerning the following question:
Problem: My script hangs during execution. Objective: I read a file (trace file of a simulation), parse it and calculate values. This trace file is the input to my script. Scenario where no problem occur:I have small trace file (with 800 lines) and run my script on that. The script produces the desired output. Scenario where problem occurs: The trace file is big (approx: 100MB) and the program stops at some point. I use another trace file which is around 80MB the program runs for some more time (than the 100MB file) and freezes. If I use another trace file which is approximately 107MB the program freezes very quickly.
I am using SUSE Linux and I use System Monitor. Before running the program CPU utilization is fine but after I run the program, when the program freezes, CPU utilization reaches 100% in one of the CPUs (I see 2 CPUs CPU0 and CPU1). The CPU is normal.
Kindly help me to make my program work. If needed I can share the code with you.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Program Hangs
by Ratazong (Monsignor) on Dec 04, 2010 at 23:09 UTC | |
Without seeing the code, the advice can only be general: You should investigate on which line of the 100MB-file your script fails. Using 100% of the CPU is an indication that the program enters an "endless" loop. Once you know the last line the script processed normally - and, more importantly, the line where you script hangs - you can investigate why it hangs... and implement countemeasures. If you want the help of us monks, this information is essential (which line won't be processed, which code do you use to process that file) All the best, Rata | [reply] |
Re: Program Hangs
by BrowserUk (Patriarch) on Dec 05, 2010 at 11:09 UTC | |
Your algorithm is fundamentally flawed. Assume for a moment that the input file consisted of the following:
Your algorithm would proceed as follows:
Note that the above has matched both S records with the same R record. Now imagine that your 100MB file has (say) 1000 S-records near the top of the file, and a single R-record 10 million lines further on. Then each of those 100 S-records would get matched against that single R-record; but you would have to re-read the 10 million intervening records over and over to do so. 1e3 * 10e6 == a very long time. It might well look like it had hung. If the S-record/R-record pairs should appear sequentially:
Then you should not be resetting the pointer after you've found the matching R-record. If however, the S-record/R-record pairs can be interleaved:
Then you would have to be maintaining two pointers: one telling you where to start looking for the next S-record; and one telling you where to start looking for the next R-record. Whilst this could be made to work, there are other, simpler approaches to this latter problem. For example: a simple, single loop down the file looking for both types of record. When an S-record is found, you push it onto an array; when an R-record is found, you shift off the least recently found S-record from the array and do your calculations.
This single pass algorithm is far more efficient, less error prone and detects mismatches. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
by anbarasans85 (Initiate) on Feb 10, 2011 at 17:52 UTC | |
The program is working fine. I modified it using PERL hash. It is fast and I am happy. This is a very late reply but I thought i can share what I did.
The logic is:
Thanks a lot for your help. | [reply] [d/l] |
by anbarasans85 (Initiate) on Dec 06, 2010 at 23:57 UTC | |
Thanks for your comments. As you said, the program is not hung. It is searching for a corresponding 'r' receive event for a 's' send event. Since it does not find any corresponding 'r'(or may find it well below) it looks like the program is hung. Yes I am aware that the program is inefficient. In spite of that I wanted this particular program to work. Then use some efficient method like the method pointed by BrowserUk or use some PERL hash. The scenario for the simulation is very simple and I thought this simulation scenario will not produce packet loss event but it did. Since there was loss of packets it resulted in 'r' events missing. This assumption about "no loss" caused the confusion about program hang. Any how thanks for your help. I will post my program here when I implement some good algorithm for this delay calculation. | [reply] |
Re: Program Hangs
by liverpole (Monsignor) on Dec 05, 2010 at 02:05 UTC | |
Seconded what ww said. You might be able to narrow down the point where the program is stuck by trapping the INT (interrupt) signal, then typing ^C when the CPU goes to 100%, and looking at the stack trace. (See caller for more details). For example:
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/ | [reply] [d/l] |
by anbarasans85 (Initiate) on Dec 05, 2010 at 04:14 UTC | |
About the statements like "very quickly" "for some more time"...:
One comment: If the program works for small trace file, why it does not work for larger file?
| [reply] [d/l] |
by liverpole (Monsignor) on Dec 05, 2010 at 04:28 UTC | |
It's not easy to tell without access to your specific data. However, one thing that strikes me as dangerous is:
Which just has "infinite loop" written all over it. As soon as you're done reading from <DATA>, you jump back to the marked $file_position which you previously saved. Try putting a print statement in the outer loop, and see if that isn't the problem. s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/ | [reply] [d/l] [select] |
by AnomalousMonk (Archbishop) on Dec 05, 2010 at 10:20 UTC | |
Using "use strict" command throws error "Bareword "SEEK_SET" not allowed ...
SEEK_SET is a constant imported from Fcntl (see seek, first paragraph): Import it and restore use strict; Update: So, WHENCE are you seeking if SEEK_SET is not defined in your program? My guess is it's the same as using 0, but that's not tested. | [reply] [d/l] [select] |
Re: Program Hangs
by ww (Archbishop) on Dec 05, 2010 at 01:26 UTC | |
"If needed I can share the code with you." Yes, some code is needed, but probably not your entire script. Rather, edit the script down to a minimum sample that creates (demonstrates) the same problem. "Show us the code" is the best advice at the moment; as ratazong says, we can't offer much more than vague generalities without code and some sample data. Also note that descriptions like "very quickly," "for some more time," and "stops at some point" are only vague generalities and thus not all that helpful in our efforts to help you. After all, is "very quickly" on the order of microseconds or days? | [reply] |