Thursday, September 30, 2010

How to debug a hanged process

Tip - It is possible to analyze threads and critical sections of process when it is in the hanged state.

Details - Follow the below steps.
1. Generate dump file of the hanged process using ProcDump(see References) utility.
2. Open the Dump file in Windbg
3. Using !locks WinDbg command,  we can list the critical sections in a dump, the thread ID of the thread owning the lock, and the number of threads waiting to acquire it.
4. Select lock owning thread View->Process and Threads menu of windbg
5. View the stack of selected thread using View->Call Stack
6. Using View menu, we can see the debug info like variables,…  of selected thread.
7. Thus we can analyze all threads that own lock

References-
ProcDump - http://technet.microsoft.com/en-us/sysinternals/dd996900.aspx
http://www.microsoft.com/whdc/devtools/debugging/default.mspx
http://blogs.technet.com/b/markrussinovich/archive/2010/08/24/3351213.aspx

Posted by - Binu Jose

Wednesday, September 29, 2010

How to view/verify Macro's code

Tip - To verify code generated by Macro, we can generate preprocessor output file and verify.

Details - We can generate preprocessor output file by using /P compiler option in VC++6(Project settings->C++->General->ProjectOptions). When compiling, a file with <filename>.i will be generated , which will contain the preprocessor output. Sample files are attached. See the Main() function in the attached files.
MacTest.cpp         - Orginal file
MacTest_TraceEnabled.i  - Preprocessor o/p file when macro is enabled MacTest_TraceDisabled.i - Preprocessor o/p file when macro is disabled

References -

Tuesday, September 28, 2010

How to detach a process from debugger

Tip - Using WinDbg we can detach a process which is attached for debugging. This is useful when debugging big projects.

Details - When debugging VC++6 projects we find it is not possible to detach a process from debugger once we start/attach to debugger. We can overcome this limitation by using WinDbg. WinDbg have almost similar interface and features of msdev debugger, but it is more powerful with features like core dump analysis. It have a menu to detach process so that we can free the process from debugger. Windbg will preserve the breakpoints we set, so that we can use them in future when we start/attach the process. Before starting debug, make sure that File->SymbolPath and File->SourcePath are set in WinDbg

References -
http://www.microsoft.com/whdc/devtools/debugging/default.mspx
http://www.codeproject.com/KB/debug/windbg_part1.aspx

Posted by - Binu Jos

Monday, September 27, 2010

How to measure GUI display time

Tip: - we can measure GUI Display/Change time without using a separate stopwatch. This is helpful especially when working with end-user GUI performance feel.
Details:- This measures time between color change of pixels in 2 targets. If we want to measure the time taken for displaying/enabling/disabling a dialog/control when a GUI operation( button click ) is done. Here we will set the (x,y) coordinates of button as Target1(generally button will have some color change to simulate the feel of being clicked). (x,y) of the dialog which is going to displayed( as a result of button click) is set as target2. Then press "Start" button of FlashTime and click on the Target1 button. When the Target2 dialog is displayed, we can see the time taken in the result area of Flash Time. If no color change is observed in Target1 or Target2 , error will be displayed. Time span between first color change of target1 and last color change of target2 calculated as time taken.

Controls in FlashTime GUI
Pitch(ms) - polling frequency in which color change is checked
Total(ms) - Maximum measurement time span Highlight Target - Puts a temporary white pixel in the target (x,y) GetColor - Gets the color of target (x,y) SetTarget - sets the (x,y) captured by dragging mouse as desired Target

Posted by - Binu Jose

Sunday, September 26, 2010

Flicker free drawing using WS_EX_COMPOSITED

Tip - With WS_EX_COMPOSITED window style is set, all descendants of a window get bottom-to-top painting order using double-buffering.

Details - This window style will give double buffering effect( somewhat like drawing with memDC ). For using this style in VC6 , #define _WIN32_WINNT 0x0500 in stdafx.h. To seeing the effect, resize the window of attached sample exe. Also see the OnCheck1() and OnPaint() in CompositeDlg.cpp to see the code behind sample application attached. Please read the MSDN link for details.

Thursday, September 23, 2010

LAMBDA EXPRESSIONS

Tip -  New Feature in VC10 - Lambdas

Details -  Simply put Lambda is an anonymous FUNCTOR (FUNCTOR  is explained in earlier post ).Many programming languages support the concept of an anonymous function. A lambda expression is a programming technique that is related to anonymous functions. An anonymous function is a function that has a body, but does not have a name. A lambda expression implicitly defines a function object class and constructs a function object of that class type. You can think of a lambda expression as an anonymous function that maintains state and that can access the variables that are available to the enclosing scope.Lambda expressions are flexible and can maintain state, just like function objects, and their compact syntax removes the need for a class definition, which function objects require. This is a new feature in Visual Studio 2010.

Example: ( See the sample implementation )

// 3rd Parameter is an lambda expression . 
// In earlier post we used a FUNCTOR as 3rd Parameter 
// [ ] is the lambda-introducer, which tells the compiler that a lambda expression is beginning.
 sort( v.begin(), v.end(),
             [ ](const string& left , const string& right)
                {          
                           return left.size() < right.size();
                  }
            );

Reference -
1. http://msdn.microsoft.com/en-us/library/dd293608.aspx
2. http://blogs.msdn.com/b/vcblog/archive/2008/10/28/lambdas-auto-and-static-assert-c-0x-features-in-vc10-part-1.aspx

Posted by - Vijesh Vijay

Wednesday, September 22, 2010

Functors

Tip -  Awareness about Functors

Details - Functors in C++ are short for "Function Objects." Function objects are instances of C++ classes that have the operator() defined. If you define operator() on C++ classes you get objects that act like functions but can also store state.Functors are an STL feature that you may not employ immediately when you start using the STL. They are, however, very useful in many situations and is an STL facility with which you should become acquainted. They give the STL a flexibility that it would not otherwise have, and also contribute to STL efficiency.  In the STL, algorithms often take a parameter which is a function (or functor) telling the algorithm how to perform some part of its task, and functors are generally more versatile and hence more useful for this purpose.

See the attached sample code for an example.

Advantages(for details see  http://en.wikipedia.org/wiki/Function_object ):
# An advantage of function objects in C++ is performance because, unlike a function pointer, a function object can be inlined.
# Another advantage of function objects is their ability to maintain a state.

Reference -
1. http://www.codeproject.com/KB/stl/functor.aspx
2. http://msdn.microsoft.com/en-us/library/aa985932.aspx
3. http://en.wikipedia.org/wiki/Function_object
4. http://www.cplusplus.com/reference/algorithm/sort/

Posted by - Vijesh Vijay

Tuesday, September 21, 2010

CTaskDialog

Tip -  CTaskDialog is a powerful replacement for the standard message boxes.

Details -   Windows Vista introduced the concept of Task Dialogs. Those are a powerful replacement for the standard message boxes.In the simplest configuration, it looks like a standard message box, but in more advanced forms it can have progress bars, custom buttons or radio buttons.The MFC in Visual C++ 2010 has a new class called CTaskDialog that is a wrapper around the task dialog API.



Posted by - Vijesh Vijay

FILE_FLAG_NO_BUFFERING and File READ/WRITE Performance

Tip -  FILE_FLAG_NO_BUFFERING FLAG can improve performance for FILE READ/WRITE operations.

Details - CreateFile API supports many flags which can be used to optimize performance for Read/Write operations. Such flags when correctly used will provide good performance compared to Normal Read/Write operations. For example , When opening or creating a file with the CreateFile function, the FILE_FLAG_NO_BUFFERING flag can be specified to disable system caching of data being read from or written to the file. It is seen that for large sized files this provides very good performance because intermediate write to cache is avoided.

An application must meet certain requirements when working with files opened with FILE_FLAG_NO_BUFFERING:
 - File access must begin at byte offsets within the file that are integer multiples of the volume's sector size.

 - File access must be for numbers of bytes that are integer multiples of the volume's sector size. For example, if the sector size is 512 bytes, an application can request reads and writes of 512, 1024, or 2048 bytes, but not of 335, 981, or 7171 bytes.

 - Buffer addresses for read and write operations must be aligned on addresses in memory that are integer multiples of the volume's sector size. One way to     align buffers on integer multiples of the volume sector size is to use VirtualAlloc to allocate the buffers. It allocates memory that is aligned on addresses that are integer multiples of the operating system's memory page size. Because both memory page and volume sector sizes are powers of 2, this memory is also  aligned on addresses that are integer multiples of a volume's sector size. An application can determine a volume's sector size by calling the GetDiskFreeSpace function.

As explained above ,Files opened with FILE_FLAG_NO_BUFFERING need to be written in chunks that are multiples of the sector size. Suppose If we have to write a huge file, but the data size  is not a multiple of the sector size , How can we write the last handful of bytes? 

Answer:- ntdll call NtSetInformationFile allows you to explicitly set the file size.  So you can write a final full sector, and then truncate using that. ( Courtesy www.experts-exchange.com )

Reference -
1. http://msdn.microsoft.com/en-us/library/cc644950(VS.85).aspx
2. http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
3. http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_20548617.html

Posted by - Vijesh Vijay

Thursday, September 16, 2010

Significance of CreateMutex API’ bInitialOwner flag in mutex creation for synchronizing the processes

Tip - It should specify FALSE as bInitialOwner  parameter while creating mutex by CreateMutex API if it is using for synchronizing the processes.

Details - Two or more processes can call CreateMutex to create the same named mutex. The first process actually creates the mutex, and subsequent processes open a handle to the existing mutex. This enables multiple processes to get handles of the same mutex, while relieving the user of the responsibility of ensuring that the creating process is started first. When using this technique, you should set the bInitialOwner flag to FALSE; otherwise, it can be difficult to be certain which process has initial ownership.


Interested people can experiment the behavior change when it set TRUE to bInitialOwner flag by referring the below steps & attached simple application.
1.    Run two instance of the attached application(name it as App1 and App2)
2.    Click Start button in App1 (it will acquire lock)
3.    Click Start button in App2 (App2 will wait for acquiring the lock. it is expected to wait until the App1 release the lock)
4.    Click End button in App1( it will release the lock. We expecting other application can acquire the lock). Try to click within 7 sec. (7 Sec is given as the timeout value in WaitForSingleObject).
You can realize that the App2 will never acquire the lock.

Refer the code CSYNCHDlg::OnStart() and modify CreateMutex call with FALSE and try again. It will work as we expecting!

Reference -
http://msdn.microsoft.com/en-us/library/ms682411%28VS.85%29.aspx

Posted by - Ajmal Karuthedath

Wednesday, September 15, 2010

DoMessageBox

Tip - Override this member function to customize your application-wide processing of AfxMessageBox calls.

Details - If you want, that every call of AfxMessageBox() in your application is replaced by the new message box, then override DoMessageBox() method in your application's main CWinApp class. And customize your message boxes or texts.  All codes of AfxMessageBox will call this method and use the new message boxes.

Reference -

http://msdn.microsoft.com/en-us/library/zf7k5hh8%28VS.80%29.aspx

Posted by - Aneesh Valeri

Tuesday, September 14, 2010

Performance Tuning with the Concurrency Visualizer in Visual Studio 2010

Tip - How to reduce the burden of parallel performance/issue analysis significantly by using concurrency Visualizer.

Details - Visual Studio 2010 includes a new profiling tool—the Concurrency Visualizer—that should significantly reduce the burden of parallel performance/issue analysis. Moreover, the Concurrency Visualizer can help developers analyze their sequential applications to discover opportunities for parallelism finding how the logical cores in the system are get utilized. I think this tool will help us to solve most of our multithreading issues.

Reference -
1. http://msdn.microsoft.com/en-us/magazine/ee336027.aspx
2. http://channel9.msdn.com/shows/Going+Deep/Visualizing-Concurrency-Inside-the-Concurrency-Visualizer-Profiling-Tool (Cannel9 presentation)

Posted by - Ajmal Karuthedath

Sunday, September 12, 2010

UWSC- a powerful scripting tool

Tip - Introducing UWSC - a powerful scripting tool.

Details - UWSC is a free scripting tool. It is very easy to use tool. We can either record the scripts or write manually.
By using the simple scripts we can perform almost all GUI actions.
Using the COM/DLL support of this tool we can extend the capability of the scripts. This tool is the core part of the well using Test Automation Framework(TAF) developed by NeST.
Of course this will save your time and help to perform many tedious operations!



How to use:
1.    Click on the Recorder button
2.    Perform the UI operations and press Alt +F2 to finish the recording
3.    Click on the Play script button to play back the recorded script

Reference -
http://www.uwsc.info/index.html

Posted by - Ajmal Karuthedath

Thursday, September 9, 2010

Parallel Pattern Library (PPL) in Visual C++ 2010

Tip - An introduction to ‘Parallel Pattern Library’ in Visual C++ 2010.

Details - Visual C++ 2010 comes with a brand new library called the Parallel Pattern Library or PPL. It is a powerful library that makes writing parallel code easier which is getting more and more important with the current and upcoming multicore CPUs. For more details please follow the links below.

Reference -
http://msdn.microsoft.com/en-us/magazine/dd434652.aspx
http://www.nuonsoft.com/blog/2009/06/12/parallel-pattern-library-in-visual-c-2010/

Posted by - Sreejith SR

OpenML (Open Media Library)

Tip - An introduction to OpenML.

Details - OpenML® is a cross-platform standard programming environment for capturing, transporting, processing, displaying, and synchronizing digital media (2D and 3D graphics, audio and video processing, I/O, and networking).

OpenML standardizes support for audio, video, 2D graphics, and 3D graphics at the lowest level that provides the desired functionality and unification. It supports a range of operating environments, from embedded systems to high-end workstations.

OpenML is a cross-platform, free, open standard so developers may download, implement and ship products using OpenML completely free of charge, royalty or licensing.

For more details please follow the links below.

Reference -
http://www.khronos.org/openml/

Posted by - Sreejith SR

Tuesday, September 7, 2010

Cloud Computing

Tip - An insight into cloud computing.

Details - Cloud computing is a general term for anything that involves delivering hosted services over the Internet.  Most cloud computing infrastructures consist of services delivered through common centers and built on servers.  Clouds often appear as single points of access for all consumers' computing needs. These services are broadly divided into three categories: Infrastructure-as-a-Service (IaaS), Platform-as-a-Service (PaaS) and Software-as-a-Service (SaaS).  For more details please follow the links below.

Reference -

http://en.wikipedia.org/wiki/Cloud_computing
http://www.wikinvest.com/concept/Cloud_Computing
http://www.microsoft.com/windowsazure/

Posted by - Sreejith SR

Monday, September 6, 2010

Flicker free drawing

Tip - Some methods for attaining a flicker free window.

Details - Flickering can be said as a sensation we get when one see an image briefly before another one is shown on top. The main cause of this behavior can be the unwanted drawing the windows are performing.  When it comes to users, this can be very annoying. There are many methods by which we can avoid this unwanted drawing. See the link below, which describes some of the cause of flicker and the cure for it to attain a flicker free window.

Reference -
http://www.catch22.net/tuts/flicker

Posted by - Sreejith SR

Sunday, September 5, 2010

Familiar with OpenMP

Tip - An Application Program Interface (API) that may be used to explicitly direct multi-threaded, shared memory parallelism.

Details - The OpenMP Application Program Interface (API) supports multi-platform shared-memory parallel   programming in C/C++ and Fortran on all architectures, including Unix platforms and Windows NT platforms. Jointly defined by a group of major computer hardware and software vendors, OpenMP is a portable, scalable model that gives shared-memory parallel programmers a simple and flexible interface for developing parallel applications for platforms ranging from the desktop to the supercomputer.

Reference

Thursday, September 2, 2010

Behind the 64MB limitation of ReadFileEx


Tip - If you use ReadFileEx API for reading a file in asynchronous mode in Windows XP 32bit OS, You cannot read more than 64MB (63.97 MB ) buffer in one API call.   Do you know why ?


Details - If you use this API in 32bit Operating System, you will get ERROR_NO_SYSTEM_RESOURCES error.  The reason for the ERROR_NO_SYSTEM_RESOURCES(1450) is that on x86 (32-bit) systems, the maximum buffer size is just under 64MB. For x64 systems, the maximum buffer size is just under 32MB.
The maximum unbuffered read and write size limits are imposed by the design of the IO manager inside the Windows executive.


The IO Manager uses the following formula to compute the maximum size MDL:


((65535 - sizeof(MDL)) / sizeof(ULONG_PTR)) * PAGE_SIZE


This formula has the following results:


Processor Page Size Pointer Size MDL calculation
======== ======== ========= =============
x86 (32-bit) 4096 4 bytes ((65535 - 28) / 4) * 4096 = 67076096 bytes (63.97 MB)
X64 4096 8 bytes ((65535 - 48) / 8) * 4096 = 33525760 bytes (32MB - 28K)


Reference - 

Wednesday, September 1, 2010

Using ReadFileEx

Tip - The IOCompletion routine will be called only when the read operation is complete and the calling thread is in an alertable wait state.

Details - Applications uses the ReadFileEx/ WriteFileEx API for reading/writing data to disk in asynchronous mode. We have to give a callback function pointer as the 5th parameter of ReadFileEx API. This call back function (IOCompletion routine) will be called only when the read/write operation is complete and the calling thread is in an alertable wait state.
The alertable wait state means, the calling thread must wait using MsgWaitForMultipleObjectsEx or WaitForSingleObjectEx or WaitForMultipleObjectsEx or SleepEx APIs.

Reference
http://msdn.microsoft.com/en-us/library/aa365468%28VS.85%29.aspx

Posted by - Aneesh Valeri