Avatar of zakiwi
zakiwiFlag for New Zealand

asked on 

How do I flush a FileStream to disk in Lazarus on Linux

Hi there,

I am processing a lot of data (100GB) and I need to write results into client files.  The resulting program itterates through the source data and for each itteration, opens the correct results file (using TFileStream) and writes the results to the file and then free's the stream.

The program works for a while, but then starts giving errors like:

Loading /old-data/2008-09-26-151035588-AC06CAC4-4C91-4FC6-B11B-B49BDDC04BF6.DAT
Processing /old-data/2008-09-26-151035588-AC06CAC4-4C91-4FC6-B11B-B49BDDC04BF6.DAT
Master: *-
Hash  : *-
Client: *Unable to open file "/new-data/client1.idx"
Unable to open file "/new-data/client1.idx"
Unable to open file "/new-data/client1.idx"
Unable to open file "/new-data/client1.idx"
Unable to open file "/new-data/client1.idx"
Unable to open file "/new-data/client1.idx"
Unable to open file "/new-data/client1.idx"

PLEASE NOTE:
- The output file does exist, and I can open it with other applcations.
- The error only happens after the program has been running for a while
- Disk activity is high during the process, which leads me to beleive that this is a flushing issue.
- If I kill the application and start again, it works for a while.
- I am running the application as root so there shouldn't be a permissions problem

I am running debian lenny.

Any help will be appreciated

Zakiwi
This is the code that is raising the error:
 
lbIndexOpen := False;
while not lbIndexOpen do
  begin
    try
      if FileExists(FileName) then
        fStream := TFileStream.Create(FileName, fmOpenReadWrite)
      else
        fStream := TFileStream.Create(FileName, fmCreate);
      lbIndexOpen := True;
    except
      on e: Exception do
        begin
          WriteLn(E.Message);
          Sleep(500);
        end;
      end;
  end;             

Open in new window

Pascal

Avatar of undefined
Last Comment
2266180
ASKER CERTIFIED SOLUTION
Avatar of 2266180
2266180
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of zakiwi
zakiwi
Flag of New Zealand image

ASKER

How do I call the flush method.  It wants a text file as input.

I accept your comments about the problem perhaps being elsewhere ... where would you look.  When the program breaks, it does not get out of the above loop so I get the error message every 500 ms

Thanks
Avatar of 2266180
2266180
Flag of United States of America image

that is the flush PROCEDURE. only classes have methods. you use it like f:tfilestream; f.flush;

I would first make sure that all my resources are protected. generally speaking:

create resource
try  
  use reasource
finally
  destroy resource
end;

if I need a try/except there, I wrap the whoel thing in a try except. I never "optimize" the number of try's when it comes to resources.
resource can be anything: memory allocation, file opening, handle allocaiton, etc.

then I look for infinite cycles. even though usually a high CPU usage will notify of infinite loops, a high disk usage in case of a disk using application can also suggest the same thing, unless the high usage is "normal". which from what you tell me I take it's not. plus that since you mention not getting out of a loop... that kind of resembles an infinite loop. that's a bug, unless, again, it's normal. I rarely saw correct code which used infinite loops in commercial applications. I saw them all the time in networking applications in school projects but I am guessing this is not the case.

so, long story short:
- you need to see what happens when thigns go wrong
- and then, try to see in code why things went wrong doing what you noticed

what usually helps me to debug such applications is using a file log and logging critical places with critical values so that I can see a big picture of what's going on.
another thing that helps in such time-critical applications is using asserts. you know for a fact that some thigns must be in some ways. assert them. if they're not, you'll get an assertion failure and that is a problem: either your thinking is wrong, or the code is. in eitehr way, you are on the correct way to find the problem.
Avatar of zakiwi
zakiwi
Flag of New Zealand image

ASKER

Mate ... there is no Flush method in the TFileStream object.

I'm using FPC 2.2.2 and Lanzarus 0.9.26 beta

Another point is that I just placed a 500 ms sleep after the file stream is destroyed  (within each processing itteration) , and disk access was notably less, BUT I still got the problem.  I think this confirms your first comment
Avatar of zakiwi
zakiwi
Flag of New Zealand image

ASKER

Thank you.  You pointed me in the right direction.  The error was infact elsewhere.

In short, I placed a counter and found that the program was breaking after a certain number of itterations and that lead me to check my file handles, and there was a leak in a FindFirst() procedure.

Cheers and thanks

Zakiwi
Avatar of 2266180
2266180
Flag of United States of America image

>> Mate ... there is no Flush method in the TFileStream object.

there is. download latest FPC and it's there. I checked BEFORE posting. if you don't want to waste time downloading, check the docs: http://www.freepascal.org/docs-html/rtl/objects/tstream.html

so, B grade is not acceptable in this situation.
1) I answered initial question for flush, correctly.
2) I pointed you in the right direction, which is kind of the only thing anybody could have done without having access to the faulty code. and in the end, I was indeed right: a resource was created and not destroyed, which was my first assumtion.

please ask mdoerators to reopen and regrade. thanks.
Avatar of 2266180
2266180
Flag of United States of America image

thanks whackamod
Pascal
Pascal

Pascal is an influential imperative and procedural programming language, largely (but not exclusively) intended to teach students structured programming and data structuring. Pasca was implemented on a wide range of architectures, from early PCs to mainframes. Pascal derivatives include OOP-based Object Pascal (used in Delphi and Lazarus/Free Pascal). Pascal is a purely procedural language and includes control statements with reserved words, but has many data structuring facilities and other abstractions like type definitions, records, pointers, enumerations, and sets.

3K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo