Question

Strange slowdown for fread.

Asked by: PaulCaswell

Hi All,

I'm not sure if this is a bug or not but it is intriguing.

I have a tool (my code) which searches for duplicate records within a file and between one file and an archive of up to 10 files.

Each file is between 80,000 and 150,000 records long and between 25mb and 50mb in size.

Duplicate checking is only required for the first 90 bytes of the record.

To avoid sorting the files I generate an index file for each data file containing the absolute offset into the data file for each record in sorted order.

The internal duplicate (look for duplicates within the file) check consistently takes just a few seconds. The external check (against archived files) takes around the same time when there are less than 5 archives to process but when 6 or more files are to be processed performance drops to a crawl!!!

Careful use of ftime has narrowed the time waster down to fread.

When running at full speed, fread takes an undectable amount of time to run with the occasional 15ms every 2000-4000 calls, probably due to cacheing by the OS.

When running like a dog, fread can consistently take 15ms every other invocation and sometimes take 30ms or 45ms.

What could make this happen? How can I avoid it?

Process Monitor reports CPU usage at around 90% when running at full speed and around 2% when running slow. No other processes show a significant amount of cpu usage but the PC runs very slowly when the checker is running slow.

I get the same kind of results when running on a quad-core 2tb-raid server as on a dual-core desktop.

Any ideas?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2007-11-28 at 07:21:41ID22987731
Tags

Microsoft

,

O/S

,

XP

,

C

Topics

C Programming Language

,

Microsoft Programming

,

C++ Programming Language

Participating Experts
4
Points
250
Comments
19

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. fread in java?
    i want to write "fread" in java c++ i s here; int fread (void * buffer, size_t size, size_t count, FILE * stream); Read block of data from a stream. Read count number of items each one with a size of size bytes from the stream and stores it in the specified buff...
  2. Quad Core Licensing for Microsoft
    Hi Experts, Had a question on licensing one quad core processor for Windows Server x32 and x64 as well as SQL Server 2005. Will I need 2 Windows Server licenses for the base OS and 2 licenses for SQL considering we go in for a per processor licensing? TIA
  3. core 2 duo upgrade to core 2 quad
    I have an ABIT LG25 with a core 2 duo. I would like to upgrade to a core 2 quad. They are both socket 775. Is it ok to remove the core 2 duo processor and plug in a core 2 quad processor?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Infinity08Posted on 2007-11-28 at 07:31:07ID: 20366113

The first idea would be that you're overloading the bandwidth to the hard-disk. What kind of hard disk do you have ? Can you monitor the hard disk read/write performance while running the tool at full speed ?

What would make this worse, is having several files open at the same time to which you are reading/writing alternately. The hard disk cache will have trouble keeping up, because it will have to switch sectors all the time ...

 

by: Infinity08Posted on 2007-11-28 at 07:32:18ID: 20366122

>> What would make this worse, is having several files open at the same time to which you are reading/writing alternately.

btw, from what I understand, that's exactly what you do, no ?

 

by: PaulCaswellPosted on 2007-11-28 at 08:33:38ID: 20366680

On disk I/O: Sysinternals process explorer shows no significant disk access for ANY process while the code runs slowly, however, the system is significantly non-reactive.

I am careful about how many file I hold open at any time. I am confident that I never hold more than 6 files open at at once. Two indexes, two data files and perhaps a couple of log files.

The really wierd thing is that I can invoke it with 5 archived files and it runs at full speed for ALL files, but run it agains 6+ it runs like a dog against ALL files. Each archive file is processed separately using a FindFirstFile/FindNextFile loop.

Are there potential side-effects to holding two FindFirstFile/FindNextFile loops running at the same time?

Thanks for your thoughts.

Paul

 

by: itsmeandnobodyelsePosted on 2007-11-28 at 09:56:12ID: 20367238

>>>> probably due to cacheing by the OS.

Maybe you answered the question by yourself. IMO, in case of running slowly the caches won't find a hit and have to read from disk again. It maybe disk caches if using one physical disc for all these files or it maybe that the stream buffers were reloaded from page file. The low CPU doesn't give a hint cause in both cases it is the (additional) disk i/o what makes it slow. You could try to open half of the archives from a network disk (best connected via a giga-bit network). If it slows down with the sixth archive as well, the problem probably is the prog and not the disc.

>>>> I get the same kind of results when running on a quad-core
>>>> 2tb-raid server as on a dual-core desktop.
Then the disc driver was not the reason.

I assume there is no multi-threading involved when accessing the archive files?  You simply check the same record against all all archive files? If so, did you try to check only 5 archive files at a time and do the rest with a separate run? Is that an option?

>>>> fread can consistently take 15ms every other invocation
Are you measuring by GetTickCount? It has a 15/16 ms accuracy only.

>>>> and an archive of up to 10 files.
Is there a reason why the archives are not appended to one big file? Or maybe a fast DB like Berkeley DB is an option. They have a very fast keyed access to their records with very little overhead.


Regards, Alex

 

by: Infinity08Posted on 2007-11-28 at 10:36:14ID: 20367558

>> Each archive file is processed separately using a FindFirstFile/FindNextFile loop.

So, you only start processing the second archived file when the first has finished, etc. ? When it comes to disk caching, there shouldn't be a difference between processing 5 or 6+ files.

Is it always the same 6th file ? Is something odd with the file (corrupted maybe, or ...) ?

What happens if you have two loops, each processing 5 files, with a pause between them ?

Just wildly guessing here ;)

 

by: evilrixPosted on 2007-11-28 at 11:32:06ID: 20367937

>> On disk I/O: Sysinternals process explorer shows no significant disk access for ANY process while the code runs slowly, however, the system is significantly non-reactive.
I have exactly the same problem when building a very large project using SCons (different scenario, same symptoms). Process Explorer shows the CPU is idle and no significant disk access; however, Perfmon (using physical disk % time counters) clearly shows that disk is being accessed 100% of the time although the HD light does not flicker either! The machine itself becomes wholly unresponsive, which I have concluded is probably down to the fact that the IO bottle neck is preventing pages from being swapped. Curiously, building the exact same project under Linux also using SCons results in no significant performance issue. I can only assume this is something specific to the caching mechanism Windows implements, to which Alex and I8 have already eluded. Of course, your problem may be wholly unconnected but I thought I'd mention it anyway as it might help guide others to a potential solution for you.

As an aside, if you are looking to implement a DB solution you could do worse than look at SQL Lite. http://www.sqlite.org/

 

by: PaulCaswellPosted on 2007-11-28 at 13:17:43ID: 20368857

Alex,

>> ... the caches won't find a hit and have to read from disk again ... try to open half of the archives from a network disk ...

Good idea, and a brilliant test. I'll try that.

>>> ... no multi-threading involved ...

correct

>>> did you try to check only 5 archive files at a time and do the rest with a separate run?

A possibility, but I would prefer to truly understand what is going on.

>>>> Are you measuring by GetTickCount? It has a 15/16 ms accuracy only.

Good point. I will switch to a finer timer.

>>>> Is there a reason why the archives are not appended to one big file? ...

Yup! They must be held completely intact, and I really dont want to add a database to the mix with all its admin headaches.

Infinity08

>>>> So, you only start processing the second archived file when the first has finished, etc. ? When it comes to disk caching, there shouldn't be a difference between processing 5 or 6+ files.

My point exactly.

>>>> Is it always the same 6th file ? Is something odd with the file (corrupted maybe, or ...) ?

Nope. Sometimes 7th, sometimes 5th. Files are not corrupt and I have checked my indexing code for potential signage issues, but I will check it again. There is a different set of files each day in a rolling set. This issue shows on every run.

>>>> What happens if you have two loops, each processing 5 files, with a pause between them ?

See above, but worth a try.

evilrix,

The problem you describe looks almost exactly like what I see. Perhaps it is more common than I thought. I will check with PerfMon. Have you tried switching off your anti-virus while your build is in progress?

All,

I think this is more than a simple cacheing issue. Process Monitor points no finger at who is taking the time, which suggests something behind the scenes. System Idle has 99%, disk I/O shows a short blip at the start of each file check but drops to zero almost immediately. Disk light flickers every few seconds.  When there are less than 5 files the graphs look just as expected, loads of disk I/O, 99% cpu usage, disk light on solid.

This is the kind of effect I would expect if there was too much cache.

Any more ideas?

Paul

 

by: PaulCaswellPosted on 2007-11-28 at 13:25:12ID: 20368926

The really wierd part is, if there are more than 6 files to process, ALL of them are slow, less than five, ALL of them are optimal.

Paul

 

by: evilrixPosted on 2007-11-28 at 13:25:15ID: 20368927

>> anti-virus while your build is in progress
Actually, I work for the AV company :)  The dev tree is excluded from the AV's on-access scanner.

 

by: grg99Posted on 2007-11-28 at 15:57:34ID: 20370218

Sounds like you're blowing your disk cache.  Things will seem mighty speedy at first as long as all the data can fit in the cache.  But eventually when you start reading file #7, the disk  cache will toss out the least recently used blocks, probably from file #1.

THat's fine if you don't ever go back to #1, but it's really bad if you're doing some round-robin processing-- the dumb disk cache algorithms will toss out just exactly what you're going to need next!! Sigh.

Also you did not mention how much memory you're using.   If your program is leaing or intentionally allocating memory, that comes out of the RAM allocated to the disk cache.


Or worse, VM kicks in and starts swapping RAM to disk, which is exactly the most pointless thing to do, exactly the opposite of what the disk cache is trying to do.

The solution is to check your program for leaks, or tighten up on any memory usage you're accumulating.


If that's not enough, you may benefit from another GB of RAM.   Under $50 these days.



 

by: PaulCaswellPosted on 2007-11-29 at 01:28:00ID: 20372291

Hi Greg,

My test setup has 1gb ram, the live setup has 8gb. I dont go anywhere near those limits. I am seeing the same effect on both.

There is little allocation/freeing of memory while the process is running.

Do you think it is possible that I am corrupting the disk cache through a dangling pointer? I would have thought that would be caught.

evilrix,

PerfMon does show memory activity and disk activity maxed out, cpu insignificant.

Could I be going too fast? Perhaps a few ms of sleep between each read may help. I'll try that too.

Paul

 

by: evilrixPosted on 2007-11-29 at 01:47:58ID: 20372366

>> PerfMon does show memory activity and disk activity maxed out, cpu insignificant
This is completely consistent with my problem!

 

by: itsmeandnobodyelsePosted on 2007-11-29 at 02:29:09ID: 20372510

>>>> Could I be going too fast? Perhaps a few ms of sleep between each read may help.
A sleep will throw your process out of schedule. It should have no impact on disk/io as it runs asynchronously.

Did you check eventvwr for some disk problems (though that wouldn't fit to same issue when using the raid)?

Are you really sure that there is no anti-virus scanner active? I experienced such kind of slowness when a scanner checked all my temporary files  while I was compiling a huge project.

 

by: itsmeandnobodyelsePosted on 2007-11-29 at 02:34:31ID: 20372522

>>>> To avoid sorting the files I generate an index file for
>>>> each data file containing the absolute offset into the
>>>> data file for each record in sorted order.
Aside that the problem should be solved somehow you should consider to using a database for your prog. Both BerkeleyDB and SqlLite don't need significant administration as they work with local database files. The efforts are less than the indexing you do now.

 

by: itsmeandnobodyelsePosted on 2007-11-29 at 02:38:06ID: 20372536

BTW, the index files were open as well? Or did you load them completely to memory? Did you ever try with smaller archive files? Or bigger ones? What is the used memory and size of the pagefile?

 

by: Infinity08Posted on 2007-11-29 at 03:59:43ID: 20372812

>> The really wierd part is, if there are more than 6 files to process, ALL of them are slow, less than five, ALL of them are optimal.

That is indeed the weirdest part : how can the first processed file be impacted by files that will be processed way later ? There's a time continuum problem somewhere heh.

That's why I don't think it's a cache issue. Is the generated code different for 5 files than for 6+ files ? Maybe the optimizer has done a very poor job, although it would need to be VERY poor for this kind of behavior.

 

by: grg99Posted on 2007-11-29 at 05:51:15ID: 20373390

you might be fragmenting memory.  go to the task manager when it's slow and peek at the performance tab.    and the processes tab, sorted by memory usage.  

 

by: PaulCaswellPosted on 2007-11-29 at 08:04:05ID: 20374640

I will certainly consider using a lightweight database tool in the future.

The indexes are left on-disk. I will see what happens if I hold them in-memory for speed.

Paul

 

by: PaulCaswellPosted on 2007-12-05 at 06:24:38ID: 20411364

OK! I seem to have removed the problem by cacheing my index entries. I chose not to cache the whole file, just 500 entries at a time.

So, yet again, I have to add code to work around OS issues. Yukk.

Paul

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...