Thread: File Output Behavior During Crash

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    6

    File Output Behavior During Crash

    Hello,

    I have a question regarding file output using <stdio.h>. First, let me give a bit of background on what I am doing.

    I am using extensive logging in a game modification to help find crashes. I am currently logging every function call and the number of writes is around 6,000 per second. As this is meant to track the last function called before a crash, the log must always be up to date when the game crashes. Currently, I am using fflush after every write to ensure this.

    My problem is that calling fflush 6,000 times per second is very slow. If I have queued writes (due to not calling fflush) and my program crashes, will they still get written properly or will they be lost? I am not concerned if the OS crashes, only the program.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    They will probably be lost, since the program crashes. So it is not a good idea.

    You could try saving the data in the memory, not in a file. Like in an array. Then at one point saving everything in a file all at once, then store everything in the memory etc etc. Don't write/read 6k/s in a file. For starters it should be really slow.
    In a way you will use an extra buffer defined by you to be big enough.

    Now, if you mean that your program crashes for other reasons, because for example your game crashes, so it reads from nowhere, you will have to check before reading from the game. If the read fails the program should close() the file after writing everything that is needed and exit.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    I see. When you say memory, you mean shared memory? I suppose that would work better (having a separate process to do the actual writing), I'll try that.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Other things I can think of are logging to a network server instead of a file (I'm not sure what kind of performance that would give you?). You can also write to shared memory and have another program that just monitors the shared memory and offloads what you put there into a file.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Why not run the code in a debugger, and just wait for it to crash?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    Quote Originally Posted by Salem View Post
    Why not run the code in a debugger, and just wait for it to crash?
    The crashes are not easily reproducible and likely caused by a combination of race conditions, so I need people without a development environment to give me the details of their crashes.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What sort of crash? What OS?

    Can you build a trap that catches the fault itself, and flushes the file before you exit? In Windows that would be a __try block (if you have "enhanced exception handling" enabble or whatever it's called), in Linux it would be a signal handler and a longjmp to get the system back to normal - it may not work 100%, but probably better than what you have right now.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > The crashes are not easily reproducible and likely caused by a combination of race conditions
    But your attempt at logging has already changed the problem.
    Changed it quite a lot it would seem.

    Also, like memory leaks, you're probably only going to discover a symptom and not a cause.
    Chances are, if it is a race condition, then it's probably not the only one either.

    You will never be able to test your way out of this problem. You'll only succeed in making the races more obscure and that much harder to track down.

    I've seen hastily thought out thread-happy code before now, and it ain't pretty.

    If you lack a solid design, which documents how resources are shared, and who has ownership of things (and how that ownership might change), then I feel all you're doing is putting sticking plaster on a patient with multiple stab wounds.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    Quote Originally Posted by Salem View Post
    But your attempt at logging has already changed the problem.
    Changed it quite a lot it would seem.
    While you are technically correct, I have not found the amount of crashes to increase or decrease after adding my logging (which I wrote so that it can be completely removed with compiler flags). The logging has been in place for several weeks and has led to the fixing of several bugs so far, so I feel it is and has been a valuable asset.

    Quote Originally Posted by Salem View Post
    If you lack a solid design, which documents how resources are shared, and who has ownership of things (and how that ownership might change), then I feel all you're doing is putting sticking plaster on a patient with multiple stab wounds.
    I understand your criticism here, though let me explain my situation a bit more.

    As I mentioned in my first post, this is actually a game modification. I (and the others I am working with) do not have full access to the source code of the game we are working with. We are working with an SDK that is not well documented, either with comments or design docs. This has led to discovering some things the hard way.

    Is this optimal? No. Is it impossible? Also no.

    So we could either try our best with what we have or just give up. We chose the former.

    Some of the errors have just been mistakes like forgetting to check a pointer that only sometimes causes a problem. If you never make a mistake when coding then I salute you, but I am unfortunately not that good.

    This log has helped us and is continuing to help us find errors. The only problem is that it slows down performance by a large amount, though not enough that it becomes unplayable or that all the errors disappear.

    Therefore I will continue with my solution because it is working for me and I do not see a better one at the moment.

    Quote Originally Posted by matsp View Post
    What sort of crash? What OS?

    Can you build a trap that catches the fault itself, and flushes the file before you exit? In Windows that would be a __try block (if you have "enhanced exception handling" enabble or whatever it's called), in Linux it would be a signal handler and a longjmp to get the system back to normal - it may not work 100%, but probably better than what you have right now.
    This is on Windows. I don't think it will be easy to trap in a try block, other than adding try/catch to every function...which I suppose is possible, with a little elbow grease.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you don't need to add try/catch on every block - just in main where you call whatever else is happening in the system - because whatever isn't caught elsewhere (and as far as I can tell, you aren't actually catching the problem), will be caught in the othermost __try/__catch block.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    Unfortunately I don't have access to the main function (Our code compiles to a DLL that is loaded), which is what I meant when I said it would not be easy to trap in a single try/catch block.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so you may be able to set up a signal() handler for SIGSEGV, and use that to flush the file and exit the application.

    http://msdn.microsoft.com/en-us/libr...12(VS.71).aspx

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    Quote Originally Posted by matsp View Post
    Ok, so you may be able to set up a signal() handler for SIGSEGV, and use that to flush the file and exit the application.

    http://msdn.microsoft.com/en-us/libr...12(VS.71).aspx

    --
    Mats
    This is perfect, thank you very much for pointing me to this Mats! I just did some testing with simulated crashes and it is working just as I'd like.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM