Discover the power of algorithms

Complexities of algorithm, thier applicability. Optimization techniques associated with diffrent algos.

Discover Power of Blogging

What is Blogging , is it a Dream or Passion or Award or Making Money ?

Think Diffrent, be creative

Let's see how much conclusion one can draw from it. This will help testing your creativity.

Discover the power of technology

Technolgy, Programming, Optimization , Gadgets and more...

Discover the power of Blogging

Google widgets and gadgets.

Showing posts with label Technical. Show all posts
Showing posts with label Technical. Show all posts

Dec 13, 2011

How to increase service start/stop timeout ?


Windows provide SetServiceStatus function by which one can manage service time out period. It’s a very straight forward API here are its details:-
         BOOL WINAPI SetServiceStatus(
                            __in  SERVICE_STATUS_HANDLE hServiceStatus,
                            __in  LPSERVICE_STATUS lpServiceStatus
                          );

To manage timeout one need to inform SCM periodically about the current status of service. So if one have pending operations same information can be conveyed with this interface to SCM.
A common bug is for the service to have the main thread perform the initialization while a separate thread continues to call SetServiceStatus to prevent the service control manager from marking it as hung. However, if the main thread hangs, then the service start ends up in an infinite loop because the worker thread continues to report that the main thread is making progress.

Here are the details of structure

typedef struct _SERVICE_STATUS {
  DWORD dwServiceType;
  DWORD dwCurrentState;
  DWORD dwControlsAccepted;
  DWORD dwWin32ExitCode;
  DWORD dwServiceSpecificExitCode;
  DWORD dwCheckPoint;
  DWORD dwWaitHint;
} SERVICE_STAT

And following are the main parameters to be used while managing service time out duration in the structure

dwCheckPoint
The check-point value the service increments periodically to report its progress during a lengthy start, stop, pause, or continue operation. For example, the service should increment this value as it completes each step of its initialization when it is starting up. The user interface program that invoked the operation on the service uses this value to track the progress of the service during a lengthy operation. This value is not valid and should be zero when the service does not have a start, stop, pause, or continue operation pending.

dwWaitHint
The estimated time required for a pending start, stop, pause, or continue operation, in milliseconds. Before the specified amount of time has elapsed, the service should make its next call to the SetServiceStatus function with either an incremented dwCheckPoint value or a change in dwCurrentState. If the amount of time specified by dwWaitHint passes, and dwCheckPoint has not been incremented ordwCurrentState has not changed, the service control manager or service control program can assume that an error has occurred and the service should be stopped. However, if the service shares a process with other services, the service control manager cannot terminate the service application because it would have to terminate the other services sharing the process as well.

A sample code showing the use of above discussed variable :-

//
// Purpose: 
//   It will set the current service status and reports it to the SCM.
//
// Parameters:
//   dwCurrentState - The current state (see SERVICE_STATUS)
//   dwWin32ExitCode - The system error code
//   dwWaitHint - Estimated time for pending operation, 
//     in milliseconds
// 
VOID ReportServiceStatus( DWORD dwCurrentState,
                      DWORD dwWin32ExitCode,
                      DWORD dwWaitHint)
{
    static DWORD dwCheckPoint = 1;
 
    // Fill in the SERVICE_STATUS structure.
 
    gSvcStatus.dwCurrentState = dwCurrentState;
    gSvcStatus.dwWin32ExitCode = dwWin32ExitCode;
    gSvcStatus.dwWaitHint = dwWaitHint;
 
    if (dwCurrentState == SERVICE_START_PENDING)
        gSvcStatus.dwControlsAccepted = 0;
    else gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
 
    if ( (dwCurrentState == SERVICE_RUNNING) ||
           (dwCurrentState == SERVICE_STOPPED) )
        gSvcStatus.dwCheckPoint = 0;
    else gSvcStatus.dwCheckPoint = dwCheckPoint++;
 
    // Report the status of the service to the SCM.
    SetServiceStatus( gSvcStatusHandle, &gSvcStatus );
}


One more important thing to remember :

Ø   Do not wait longer than the wait hint. A good interval is one-tenth of the wait hint but not less than 1 second  and not more than 10 seconds.  
          Example:- 
 
        dwWaitTime = ssStatus.dwWaitHint / 10;
 
        if( dwWaitTime < 1000 )
            dwWaitTime = 1000;
        else if ( dwWaitTime > 10000 )
            dwWaitTime = 10000;



The following are some of the best practices when calling SetServiceStatus function:
  • Initialize all fields in the SERVICE_STATUS structure, ensuring that there are valid check-point and wait hint values for pending states. Use reasonable wait hints.
  • Do not register to accept controls while the status is SERVICE_START_PENDING or the service can crash. After initialization is completed, accept the SERVICE_CONTROL_STOP code.
  • Call this function with checkpoint and wait-hint values only if the service is making progress on the tasks related to the pending start, stop, pause, or continue operation. Otherwise, SCM cannot detect if your service is hung.
  • Enter the stopped state with an appropriate exit code if ServiceMain fails.
  • If the status is SERVICE_STOPPED, perform all necessary cleanup and call SetServiceStatus one time only. This function makes an LRPC call to the SCM. The first call to the function in the SERVICE_STOPPED state closes the RPC context handle and any subsequent calls can cause the process to crash.
  • Do not attempt to perform any additional work after calling SetServiceStatus with SERVICE_STOPPED, because the service process can be terminated at any time.



Nov 22, 2011

How to start , stop perfmon from the command line in Windows ?

Logman command is the solution for it.
let's have a look at the command syntax

Syntax Verbs
Logman [create {counter | tracecollection_name ] [start collection_name] [stop collection_name] [delete collection_name] [query {collection_name|providers}] [update collection_name]

Parameter details:-

create {counter | tracecollection_name Creates collection queries for either counter or trace collections. You can use command line options to specify settings.
start collection_name Starts the data collection query collection_name. Use this option to change from scheduled collections to manual ones. Use the update parameter in the command line with begin-time (-b), end-time (-e), or repeat-time (-rt) to reschedule collections.
stop collection_name Stops the data collection query collection_name. Use this option to change from scheduled collections to manual ones. Use the update parameter in the command line with begin-time (-b), end-time (-e), or repeat-time (-rt) to reschedule collections.
delete collection_name Deletes the data collection query collection_name. If the collection_name does not exist, you will receive an error.
query {collection_name|providersIf no collection_name or providers are given, the status of all existing collection queries are displayed. Use collection_name to display the properties of a specific collection. To display the properties on remote computers, use the -s remote computer option in the command line. Use providers as your keyword in place of collection_name to display the registered providers installed on your local system. To list registered providers installed on the remote system, use the -soption in the command line.
update collection_name Updates collection queries for counter and trace collections. For counter collections, modifications to the query will stop, and then restart the collections. For trace collections, use the following parameters in the command line to query without stopping the collection: -p provider [(flags[,flags ...])Level- max n- o PathName-ft mm:ss, or -fd.


This command is very helpful in performance automation scenario.
If performance monitoring counter name is test_perf_log
then start command will go as :-
    logman start test_perf_log
on similar line stop command will go as:-
    logman stop test_perf_log


For more detail jump to page :- http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/nt_command_typeperf.mspx?mfr=true



Sep 1, 2011

Online Regular Expression / Regex Tools and Editor

Some useful compiled link about online regex tester :-
  • Regex tester from Regular Expression Info.com

             http://www.regular-expressions.info/javascriptexample.html


RegexBuddyInteractively create and test regular expressions with RegexBuddy.
Create and analyze regex patterns with RegexBuddy's intuitive regex building blocks. Quickly test regular expressions on sample data and files in a safe sandbox. Debug regexes easily with real-time highlighting and informative regex match details. Get your own copy of RegexBuddy now.









  • Online regular Expression tester from PageColumn.
           http://www.pagecolumn.com/tool/regtest.htm
          A very fast and effective regex tester.
  • FileFormat.Info
            http://www.fileformat.info/tool/regex.htm

Table For regular expression syntax:-
            Regular Expression Syntax :- http://msdn.microsoft.com/en-us/library/1400241x(v=vs.85).aspx



Mar 5, 2010

What is Inline function in c/c++? A discussion on its Advantages,Disadvantages,Performance and Uses Guidelines.

Inline function is the optimization technique used by the compilers. One can simply prepend inline keyword to function prototype to make a function inline. Inline function instruct compiler to insert complete body of the function wherever that function got used in code.

Advantages :-
1) It does not require function calling overhead.
2) It also save overhead of variables push/pop on the stack, while function calling.
3) It also save overhead of return call from a function.
4) It increases locality of reference by utilizing instruction cache.
5) After in-lining compiler can also apply intra-procedural optimization if specified. This is the most important one, in this way compiler can now focus on dead code elimination, can give more stress on branch prediction, induction variable elimination etc..



Disadvantages :-
1) May increase function size so that it may not fit on the cache, causing lots of cahce miss.
2) After in-lining function if variables number which are going to use register increases than they may create overhead on register variable resource utilization.
3) It may cause compilation overhead as if some body changes code inside inline function than all calling location will also be compiled.
4) If used in header file, it will make your header file size large and may also make it unreadable.
5) If somebody used too many inline function resultant in a larger code size than it may cause thrashing in memory. More and more number of page fault bringing down your program performance.
6) Its not useful for embedded system where large binary size is not preferred at all due to memory size constraints.








Performance : -
Now covering the topic which most the people are interested in the "Performance".
In most of the cases Inline function boost performance if used cautiously as it saves lots of overhead as discussed in our Advantages section above but as we have also discussed  its disadvantages one need to be very cautious while using them. Today's modern compiler inline functions automatically, so no need to specify explicitly in most of the cases. Although placing inline keyword only gives compiler a hint that this function can be optimized by doing in-lining, its ultimately compiler decision to make it inline. Though there are ways to instruct compiler too, for making a function call inline like one can use __forceinline to instruct compiler to inline a function while working with microsoft visual c++. I suggest not to use this keyword until you are very sure about performance gain. Making a function inline may or may not give you performance boost, it all depends on your code flows too. Don't expect a magical performance boost by prep-ending inline keyword before a function to your code as most of the compiler nowadays does that automatically.

As we have seen inline function serves in terms of performance but one has to use it with extreme cautions.

I have prepared a few guidelines for its use.
Uses Guidelines :-
1) Always use inline function when your are sure it will give performance.
   Recently i got very good example showing immature use of inline function.
Suppose you have used inline function and for some reason compiler rejects it.
Well, when the compiler cannot inline a function, it generates a static definition of the function instead. In other words, the code for the function is generated just like the code for a static function. This means that every translation unit in which an inline function cannot be inlined will get its own copy of the function, which can increase the size of your program. Most compilers will report warnings when a function cannot be inlined. You should pay attention to these warnings to determine if some functions should not be inlined in the first place.

There is one more detail with inline functions that you should be aware of. Suppose you have chosen to use a local static variable in an inline function:

inline void foo()
{
static int index;
// ...
}

Next, suppose for some reason, the compiler cannot inline this function in several translation units. Each translation unit will then have its own static definition of foo(). But, this means that each version of foo() may also have its own copy of the static variable index! Clearly not what you want. So avoid putting static variables inside of non-member inline functions.
2) Always prefer inline function over macros.
3) Don't inline function with larger code size, one should always inline small code size function to get performance.
4) If you want to inline a function in class, then prefer to use inline keyword outside the class with the function definition.
5) In c++, by default member function declared and defined within class get linlined. So no use to specify for such cases.
6) Your function will not be inlined in case there is differences between exception handling model. Like if caller function follows c++ structure handling and your inline function follows structured exception handling.
7) For recursive function most of the compiler would not do in-lining but microsoft visual c++ compiler provides a special pragma for it i.e. pragma inline_recursion(on) and once can also control its limit with pragma  inline_depth.
8) If the function is virtual and its called virtually then it would not be inlined. So take care for such cases, same hold true for the use of function pointers.


That's it from my side, I hope you enjoyed reading the post.



Mar 2, 2010

How to achieve code portability ? Basic 10 quick tips.

Code portability basically refers to making source code able to compile on different platform without making any changes in source code.
While coding its very important to keep portability in mind.The best way to introduce code portability is while coding.Keeping certain things into account we can achieve code portability with lesser effort, which we will discuss in this post.There are certain tools too which detect portability problems on source code, its a post processing of code and requires extra effort.
Non-portable code introduces problems like maintenance of different versions, reduces readability, reduces understanding of code etc...
Efforts needs to make legacy or old source code portable, can really make you feel lost in this big programming ocean. So, the best policy is to keep portability into account while writing code, it saves lots of time and efforts on rework. Big question now is - "How to write portable code?".Our source code should be compatible with different environment like different processor, different OS, different version of libraries etc... In this post we would focus on basic tips need to be kept in mind while writing code. 

1) Don't assume data type size to be constant across platform, as it may change with platform.
             Many a times programmers makes a common mistake by assuming size of pointer and long same.If in some expression sizeof(long) is used, it may give different result on 32-bit and 64-bit OS version. Like if we talk about Microsoft Visual Studio running on 64-bit OS version the pointer size would be 8 byte and size of long comes out to be 4 byte. Program written with such assumption would give false result or may even get crash.So, one has to be very cautious while using data type size across the platform.

2) Don't use specific system constants.
            System specific constant should not be used as they are not portable, we are some time not aware of them also. 

3) System file/folder path notation may vary on different platform.
           When working with file path one need to be cautious for example "\\testfolder\\TestFile.txt" will work on Windows but give error on Linux.For this one i recommend to use forward slash "/tesfolder/TestFile.txt" , it would work well on both windows and Linux.

4) Avoid using system specific models/libraries.
         Don't use system specific models/libraries like Event handling model, Threading libraries, File Creation libraries etc.. . As they are not compatible across platform. Write a wrapper around such models and within wrapper use generic portable libraries. For example, Windows even handling model is totally different from Linux. Windows have special mode for handling events, like we may not find timed wait for multiple object on other platform.

5) Always write default statement in switch case.
         Many latest compiler gives compilation error if default is not specified.

6) Always specify return type for functions. 
         Many latest compiler gives compilation error if return type is not specified.

7) Always specify type with static variables.
         Variables declared with static keyword must contain data type with it, some old compiler take int as default type but modern compiler will generate compilation error for it.

8) Always take care of scope of variable.
        Like some compiler support variable scope limited to for() while some compiler dont.
    For example:-
        Don't prefer writing code as below (Non-portable code).
        {
           for(int i ; ;)
          {
          //do some thing
           }


           for(int i ; ;)
         {
          //do some thing
          }

        }


         Prefer writing code as below (Portable code)
        {
           for(int i ; ;)
          {
          //do some thing
           }

           for(int j ; ;)
         {
          //do some thing
          }

        }

9) Don't use C++ commenting style in C code.
      Don't use // commenting style in c code, as compile other then microsoft visual studio may generate error for it. Prefer using /* */ commenting style.

10) Take care of include depth for header files and also for file code size.
       Microsoft visual studio compiler generated error like "internal compiler error"  if include depth is too large or file size exceeds certain limit. Always take care of file size and include depth. 

I have tried to cover 10 basic tips for code portability for beginners though there are several other areas too, where we need to focus on advanced portability issues, for e.g. dealing with classes, virtual functions, exception handling, compiler directives, run-time identification. I will cover this topic separately bye for now.
  
Hope you enjoyed this post !

Keep Rocking
-Tajendra



What's the difference amongst code portability, software portability and application poratoability ?

Many people gets confused of terms Code Portability, Software portability and Application portability.
Lets understand the differences amongst them. Application portability means that application is designed in a way that it can run independently without actually installing its file on the system. A portable application did not install any file on the system and can be run with the help of removable devices like USB, portable hard drive, CD, flash drive or floppy disk. Application portability is often get confused with Software portability, where as Software portability refers to designing of software in a way that its source code can be compiled on different platform without any changes. Software portability is also referred as code portability, both the terms can be used alternatively. Application portability does not implies that it can run on multi-platform, it only implies that it runs independently on system from its storage location only. Portable application saves or read its configuration from its storage location, it does not leave any files on the system its running upon. This feature make the application portable.
So now difference is very clear between software portability and application portability.

Hope you enjoyed the post !
-Tajendra



Feb 27, 2010

Understanding which is faster Post or Pre increment operator ( i++ vs ++i ) ?

In c++ pre and post operator are used with variables where one need to just increment or decrement value by 1.For incrementing value ++ operator can be used and for decrementing -- operator can be used.
Its use is very simple one can easily say ++i or i++, to increment its value by 1. Many a times i found people very confused about it's usage (at beginners level) and some professional showing lots of concerns about its performance .That's what made me writing this post.
lets focus on its use first:-
One can now ask question that why there are two different version present for if we simply need to increment or decrement the value.
Lets take one example to see its effect in expression for post and pre increment operator :-

a) x = ++y; //pre increment
b) x = y++;//post increment

For above two expression value of x will be different. In expression (a) value of y would be incremented first then it would be assigned to x. And in expression (b), x would be assigned value of y first and then y would be increment. So if the value y is 5, then value of x would be 6 in expression (a) and its value would be 5 in expression (b).




We are now aware of its usage and impact in an expression, now lets find out which is better ?
To a much extent we can say that pre-increment is faster then post-increment operator. If we talk about independent statement containing just pre-increment and post-increment operator then pre-increment is faster (without compiler optmization).but, if we talk about integer variable the difference is almost negligible.It would be one or two cycle in unoptimized c++ code.As with pre-increment operator we did not need temporary location to store value, value is directly incremented and assigned where as in post-increment operator we need to store value in temporary variable. So with data type like integer, pointers no major performance can be achieved but if we talk about classes one should prefer use pre-increment operator.
Lets try to implement both operator it would help us gain understanding about it :-
pre-increment operator
++i
int PreIncrement(int i)
{
  i = i+1;//incrementing i
return i;
}

post-increment operator

i++
int PostIncrement(int i)
{
  int tempvar = i;//temporary storage
  i = i + 1;//incrementing i
  return tempvar; //return tempvar
}


With above example its very clear that pre-increment is better and efficient then post-increment operator.
Again i want to make one point clear that it would not improve performance for data type like integers, pointer with advanced compiler you will not see any difference in assembly code too.I have written one test code to show this, lets look at it :-



int x =0;
001F361C  mov         dword ptr [x],0 
int y =1;
001F3623  mov         dword ptr [y],1 
x = ++y;
001F362A  mov         eax,dword ptr [y] 
001F362D  add         eax,1 
001F3630  mov         dword ptr [y],eax 
001F3633  mov         ecx,dword ptr [y] 
001F3636  mov         dword ptr [x],ecx 
x = y++;
001F3639  mov         eax,dword ptr [y] 
001F363C  mov         dword ptr [x],eax 
001F363F  mov         ecx,dword ptr [y] 
001F3642  add         ecx,1 
001F3645  mov         dword ptr [y],ecx 
y++;
001F3648  mov         eax,dword ptr [y] 
001F364B  add         eax,1 
001F364E  mov         dword ptr [y],eax 
++y;
001F3651  mov         eax,dword ptr [y] 
001F3654  add         eax,1 
001F3657  mov         dword ptr [y],eax 


With above disassembly code its clear that is no much impact on assembly instruction, code generated is almost same for both the cases.With all the analysis done so far, i am still thinking about post-increment operator.All points are going in-favor of pre-increment operator but there should be some benefit of post-increment operator too, where it can be used. Finally i found one such condition where post increment is better then pre-increment operator, lets look at following example :-
a) x = arrIntValues[i++]
b) x = arrIntValues[++i]

In above case expression (a) is better than (b) because in the second case,the address calculation  of the array element has to wait for the new value of i which will delay the availability of x for one or two clock cycles.But it should be used cautiously since if you are making such changes in programs it may change code behavior.

Time to conclude now. As we have seen usage and comparison of pre/post increment operator, which shows that both are equally good if used for primitive data types with optimized compiler option.For non primitive data type one should use pre-increment operator. And finally it depends upon your code condition too, as we have seen one condition where post-increment is more efficient then pre-increment operator.Whole discussion    is also applicable for pre/post decrement operator too.

Hope you enjoyed the post, Cheers,
-Tajendra




Feb 24, 2010

How to allocate memory dynamically on stack ?


I read some time back that memory operation on stack is much faster than heap. With this
information the first thing came into my mind was that if we get a way to allocate memory
dynamically on stack it would surely help us to optimize memory operation cost. I started
searching for it and finally I found one such way J.
Before going into its detail let’s take a look at dynamic memory allocation on heap.

Generally when it comes to dynamic memory allocation new/malloc  the well known calls
Comes into mind. They help us to dynamically allocate memory on the heap.

For e.g.
  int FunctionA()
{
   char* pszLineBuffer = (char*) malloc(1024*sizeof(char));
    …..
  // Program logic
     ….
  free(pszLineBuffer);
  return 1;
}

Above code would allocate 1024 bytes on heap dynamically. After the complete use of variable szLineBuffer, we
need to free memory also (free/delete can be used for it).  So, one has to  keep track of deallocation call,
else memory leak will get introduced.

Now coming back to our question, is there any way by which we can allocate memory dynamically on stack.
So the answer is yes. We can allocate variable length space dynamically on stack memory by using function
_alloca. This function allocates memory from the program stack. It simply takes number of bytes to be allocated and
return void* to the allocated space just as malloc call. This allocated memory will be freed automatically on function exit.
So it need not to be freed explicitly. One has to keep in mind about allocation size here, as stack overflow exception may
occur. Stack overflow exception handling can be used  for such calls. In case of stack overflow exception one can use
_resetstkoflw() to restore it back.

So our new code with _alloca  would be :-

  int NewFunctionA()
{
   char* pszLineBuffer = (char*) _alloca(1024*sizeof(char));
    …..
  // Program logic
     ….
  //no need to free szLineBuffer
  return 1;
}

Now let’s check out what will be the advantage of _alloca over new/malloc :-
1)      We have saved overhead of new/malloc.
As _alloca() got very little overhead of allocating memory on stack.
2)      No need to free memory explicitly. So, deallocation cost will be zero.
3)      If function like FunctionA() got multiple time in your program it may cause heap memory fragmentation on a long run,
Which would be saved with NewFunctionA() as stack memory never goes fragmented.


Now let’s check out what will be the disadvantage of _alloca over new/malloc :-
1)      One has to be cautious while using alloca for huge blocks.
2)      Its scope is limited to a function call.
3)      As stack overflow is not a standard C++ exception, one hast to use structured exception handling for it.


I hope you enjoyed this post.
-Tajendra