Saturday, July 14, 2012

Selecting a high-end software development computer

Computers can be tagged as a multipurpose device. Nowadays it is being used in all the profession. Also, there have been lots of changes in the way computers are built in the present era. Their overall architecture evolves very rapidly. Someone from non-IT background asked me about the ideal configuration of the computer that he can own and my answer to that was “it depends”. The requirements and the usage scenario matters a lot while selecting a computer or a notebook. As an example, the journalist may need a computer to maintain the track of the notes and upcoming unpublished articles. The basic configuration is enough to satisfy his needs. On other hand, the software developer may need an advanced configuration because he is expected to run a whole lot of his programming software and some in background as well. Once we narrow down on our usage scenarios it becomes very easy to select one from the authorized reseller. Let’s discuss each and every component of the computer and decide upon the ideal configuration. We will also discuss some of the key terminologies used by hardware vendors for marketing their products.

Microprocessor (Category: Performance): As we all know it is the heart of the computer. It synchronizes the operations of all the hardware devices present on the mainboard. For a developer, the ideal processor configuration would be 3 GHz (minimum) because the development tools are getting feature rich day by day. These tools consume lot of CPU cycles to provide the smooth operation to its users. The processor L3 cache also contributes to the speed to some extent. The number of cores is equally important along with the individual core speed. This is due to lot of programs being run at the same time e.g. lots of windows services runs in background while computer is in use, anti-virus real time scanning, outlook for emails, some music, developer’s tools, etc. These softwares perform better when they are allocated a separate core to run them. Their stability also increases a lot if ran on separate core. The processor with thread level parallelism performs even better. This essentially means that n number of threads can run on the same CPU core and hence can utilize the core speed to the fullest. If the development scenario requires virtualization then the virtualization enabled processor can be selected. These processors are programmed to provide better performance, reliability and security while working on the virtual machines. The processors are categorized as 32-bit and 64-bit. The 64-bit processors are required in case the programs needs more physical memory to operate e.g. opening 3 GB log files from production environment or attaching a large files in the email or using lots of extension plugins to extend visual studio IDE, etc.

This lets us to put the configuration for the ideal development machine is: 3GHz - x64 – 4 cores/8 threads – VT

Physical memory – RAM (Category: Performance): The development machine needs more RAM as compared to a non-development machine. The ideal development machine runs lots of programs including virtual machines hence they consume more RAM. I would put in 8GB RAM as bare minimum requirement to run a high end development computer. The RAM speed – FSB (front side bus) is equally important for the RAM to operate at the speed of processor. E.g. 800 MHz RAM will waste more CPU core cycles as compared to 1066, 1333, 1600, 1866, 2166 MHz RAMs. The metrics to identify this efficiency is known as bus-core ratio. This metrics is associated with the processor because it defines the efficiency of the processor. The bus-core ratio can be explained as the number of front bus cycles consumed per number of clocks generated by processor. For a developer machine it is advisable to keep it to maximum compatible value. The RAM processes the data at 1’s of the clock. Some RAM’s processes the data at the positive slope of the clock only which are known as DDR2 whereas some processes the data at both the positive and negative slopes which are termed as DDR3. Due to above reason DDR3 performs twice as better as compared to DDR2. This helps us to categorized RAM as DDR2 or DDR3. The development machine shows up considerable responsiveness while running the softwares on DDR3 RAM.

This lets us to put the configuration of RAM as DDR3 - 8GB – more than 1333 MHz FSB.

Hard drive (Category: Performance): It depends on the number of softwares a development machine is expected to store. It contributes to the performance of the machine to a great extent. Based on their architecture they are categorized into two types: HDD and SSD. HDDs have rotating platters to store the data. The data is retrieved by the flux generated by rotating magnetic platters. The SSD do not have any rotating parts but it contains the chip design logic to store the data. SSD’s are 10 times faster than HDD but they are 4 times more costly. During the operation of the computer the major bottleneck could be the reading of the data from the hard disk in case of page faults. SSD can speed up the data fetch as compared to HDD and thereby improves the performance of the machine considerably giving better responses while doing software development.

This lets us to put the configuration of hard drive as SSD – 120 GB

Notebook screen (Category: Display): The screen of the notebook should be non-reflective while developing a software otherwise it may not be operated smoothly in the natural light. The non-glossy screens, however, reflects the colors in a dull shade. I feel this matters a least in software development scenario because software development is all about writing a code rather than building a user interface. This may not hold true for content writers or graphics designers and they may have to opt for glossy screens

This lets us to put the configuration of screen as 15.6” Matte finish

Graphics processing unit (Category: Display): The GPU matters a least in a software development machine. Let’s select the default option

Keyboard (Category: Operation): The keyboard is the equally important component while selecting the software development machine. Make sure the keyboard keys layout is exactly same as the one you are using right now. E.g. placement of delete or insert key is not different otherwise you may have to put extra efforts to remember the new locations of each key. The keyboard may be a backlit one if you are going to work in dark. The separate numeric pad may or may not be present as it is seldom used during software development. Another important requirement of software development is the usage of the function keys. The function keys should not be clubbed with multimedia buttons otherwise the user has to press fn key along with function key to make it work while debugging or applying any shortcut using function keys.

Speakers (Category: Operation): Only if you are a music fan. Select the default one as there are no options available

Number of USB (Category: Operation): The number of USB matters a lot because some developers have a habit of using external mouse, keyboard, and USB modem along with USB headsets, printer/scanner, USB device chargers. Hence it is advisable to get as many more USBs as possible. The ideal count would be 4-5.

The excellence of the development machines lies in their individual components and hence they should be picked up with utmost care. The developer should avoid getting attracted by the marketing tag lines of providing a fastest computer on the planet. Instead have a look at the individual components and compare it with the other brands and select the one that suits your requirements. It is also true that the most advance computer of today’s date will get outdated 3 years down the line and hence needs to be upgraded or a new purchase. Hope the post covers everything to look for while purchasing a new development machine.

Friday, July 6, 2012

Multithreading & parallel processing

Multithreading is the feature of the programming language that enables the developer to do the parallel processing in the software. You can listen to music while coding! The operating system creates process for each of the task to perform in parallel. The process is scheduled to run on the processor. The process can have one or multiple threads. The execution logic can be divided into multiple threads to perform the concurrent execution. e.g. The Dal thread can fetch the data from the database and UI thread populates them on the UI thereby reducing the wait time of the user to see the results. The programming languages provides lots of API's to operate on the threads and to manage them. In this blog post, lets discuss about the threads and paradigm of parallel programming.


In computer science terminology a thread of execution is the smallest unit of processing that can be scheduled by an operating system. A thread is a lightweight process. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process. (ref Wikipedia)

In terms of programming language, there can be two types of thread - main thread and worker thread. The program is run in the main thread. The worker thread is created from the main thread to share the workload of the main thread. The worker thread is joined to the main thread to consume the result in the main thread. The figure below depicts the scenario of the multithreaded application



The figure shows the main thread spawning the worker threads to execute the code in parallel. The worker thread executes the logic independently of each other and finally joins the main thread to proceed the further execution on the main thread. The main thread is blocked until both the worker threads joins the main thread. The worker threads are allocated separate CPU core to execute the logic at the full core speed. This is the main reason why multithreaded application performs better than single threaded application. The sample C# code for multithreaded application is as shown below:



The main thread creates two threads viz. thread and thread1. These threads have a worker method associated with them. The threads starts execution when Start() method is called on each one. The sequence in which the thread starts and completes the execution is non-deterministic and depends solely on the processor and thread management component of CLR.

.NET framework methods to operate on thread:

  1. Start - Start the thread execution
  2. Suspend - Suspend the thread execution
  3. Resume - Resumes the execution of the suspended thread
  4. Abort - Terminates the thread

Race condition:

The non-deterministic execution behavior of the thread results in the condition which is often referred to as race conditions. In race conditions the final output may vary depending upon the sequence of the execution of the thread. Hence every run of the program may result in different output. The race conditions can be avoided by using synchronization techniques between the producer and consumer threads

Inter-thread and inter-process communication:

As discussed in above sections, the thread is an independent unit of execution. The worker method is expected to have all the data required for it to execute independently and flawlessly. However there are scenarios where one worker thread may depend on the data that is generated by another worker thread. Hence there needs to be a communication channel between worker threads. This mechanism is known as inter-thread or inter-process communication. Wikipedia explains it as - "In computing, Inter-process communication (IPC) is a set of methods for the exchange of data among multiple threads in one or more processes". There are various types of IPC methods: message passing, synchronization, pipes, shared memory, etc.

  1. Message passing: The mechanism to enable the thread to exchange the messages between each other. The messages should be generated based on the predefined schema and so that they it can be interpreted by the consumer threads. The typical example of message passing mechanism is MSMQ. The MSMQ stores the messages in the xml format. The format should be well understood by the consumer threads so as to enable consumer thread to read the messages that are generated by producer thread

  1. Synchronization: The mechanism by which the thread synchronizes themselves with the other threads so that all the threads uses the fresh and accurate copy of the data to avoid discrepancies in the final output is known as synchronization

  2. Pipes: This methodology sequences the thread execution and pass the result of one thread as an input to other.

  3. Shared memory: This methodology reserves some amount of the physical memory for all the threads to spool the data which can be shared by other threads. The memory area needs to be protected so as to ensure that one thread enters the shared memory at any point of time. These are often referred to as critical sections.

Let's discuss synchronization and shared memory concepts in details.

Thread Synchronization: The thread synchronization is the mechanism with which the thread suspends or resumes its processing based on the state of the other thread. This helps the thread to operate on the correct copy of the data generated by the other threads. This is achieved by ensuring that only one thread enters the critical section and operates on the data while other threads waits (blocks) for the previous thread to exit the critical section. The thread signals the other threads when it exits the critical section so that the other threads which are waiting for the previous thread get chance to enter the critical section and operates on the data. There are several methodology to perform thread synchronization viz. mutex, semaphore, monitor

Types of thread synchronization:

The thread synchronization helps to maintain thread safety. The various methods to achieve thread synchronization is as follows:

  1. Mutex: The flag used by the threads to stop the other threads from entering the critical section. The thread sets the flag after it has entered the critical section and resets it when it exists the critical section. The other threads wait (busy waiting) on the flag and checks whether the flag has been reseted. They get chance to enter critical section when they see the flag has been reseted by the previous thread. In terms of programming language the mutex is essentially a boolean variable that hold the value of true/false.

  2. Semaphore: It is the special implementation of mutex except that it maintains the available count of the resources. This enables the operating system to avoid the race conditions because it predefines the number of resources that can be allocated. These are known as counting semaphores.

  1. Monitor: In concurrent programming, a monitor is an object or module intended to be used safely by more than one thread. The defining characteristic of a monitor is that its methods are executed with mutual exclusion. That is, at each point in time, at most one thread may be executing any of its methods. This mutual exclusion greatly simplifies reasoning about the implementation of monitors compared to reasoning about parallel code that updates a data structure (ref: Wikipedia)

.NET framework implementation of the thread synchronization and thread safety

  1. Lock: This keyword is used to ensure that only one thread enters the critical section. The lock is internally compiled as the monitor. The lock operates on the object type data structure. The sample code may look like as follows:



  1. Events: The events are the messages that are communicated between two threads. The event has two states: Wait and Signal. The wait state causes the thread to wait till the other thread does not signal it. The signal state enables the thread to enter the critical section which means the previous thread has exited the critical section. There are two kinds of synchronization events in .NET:
    1. AutoResetEvent: The event is signaled automatically if the thread is waiting on auto reset event and the auto reset event returns to un-signaled state after the thread is allowed to enter the critical section. The thread needs to call Set() to explicitly signals the auto reset event which then allows other threads to enter the critical section



  1. ManualResetEvent: The thread resets the event and enters the critical section. After completing the execution the thread calls Set() to allow other waiting threads to acquire the lock to enter critical section.


  1. Monitor: The monitor is implemented using Enter() and Exit() methods on the Monitor class in .NET. The critical section is placed between Enter() and Exit() methods so that monitor ensure only thread enters the critical section at any given point of time.


Thread pooling:

The thread pooling is the mechanism with which the thread is allocated from the list of available threads to execute the user work items. The ThreadPool class in .NET enables the framework to spawn and schedule the thread for individual work items of the user. Essentially the thread pool is implemented as a queue (FIFO). The scheduling of the user work items is handled by operating system and there is no guarantee that the thread is scheduled immediately after adding the user work item to the queue. The thread returns back to the thread pool once it completes the current work item for the user so that it can be scheduled once again after sometime.


Writing the thread-safe code:

The thread-safe code is defined as the code that does not allow other threads to modify/update the data used by current thread. The code is thread-safe only if it safely manipulates the data in the shared memory without hampering the execution of the other threads. There are numerous ways to achieve thread safety in the multithreaded application. Let's discuss the obvious approach to write the thread-safe code i.e. reentrant code. For other methodologies to write the thread-safe code please refer to section: .NET framework implementation of the thread synchronization

Reentrant code: The reentrant code is the code which gets the data from the main/parent thread for itself to run independently. The thread maintains its own copy of the data instead of using the global data. This helps the thread to avoid using the data that can be modified by other threads because all the thread has local copy of the data. The reentrant code executes with multiple threads and ensuring the result is consistent across the thread execution. This is achieved by storing the data locally on the thread stack instead of global variables

Deadlock:

The system is said to be in deadlock if each thread is waiting for the other to release the non-preemptive resource and there is a circular wait amongst these threads. It is very critical to detect the deadlock and prevent it from happening because the system can go into endless wait and never comes up again. The detailed discussion of the deadlock is outside the scope of this blog post.

Multithreading on single-core and multi-core machines:

The multithreaded application does not yield good results when run on the single-core machine the code is run on the single core. There is also an overhead involved in managing the thread which is done by CLR e.g. context switching, loading the state from previous run, etc. The thread execution on the single core machine happens on the concept of the time-slice wherein all the thread executes on the processor for a given time span before the next thread is scheduled. The multithreaded application performs better on the multicore machines. The multicore machines has an ability to schedule the threads on the different cores hence achieves the parallel processing in the true sense


Usages of the multithreading concept:

Most of the softwares especially the games and the enterprise products available today makes use of heavy multitasking. These softwares needs a very high quality of the hardware configuration including the multicore processors.

Future of the multithreading:

Nowadays the multithreading has become a basic necessity of any software. There has been a great evolution in the processors as the manufacturers are putting more cores on the processor die. This facilitates the software writers to use multithreading extensively in their products to increase the responsiveness of the product.

Sunday, February 19, 2012

Implementation of SQL cache dependency in the .NET framework

Today's world is moving very fast due to the development and evolution of computer and software. The software ensures that the business process runs better, smooth, and reliably to ensure the required information is available at any time and at the faster rate. Nowadays, the organizations are using more electricity than paper. Most of the organizations runs the software applications that executes on database/data stores. The database is very critical for any business intelligence application to work smarter. These applications processes the large chunk of data every second and produces the information to make decisions based on it. The application performance may reduce if the average number of database calls increases because the processing happens outside the application server machine boundary. It becomes very important to monitor the number of database calls that are made from the application for the better performance. This is achieved using caching.

Caching is the process by means of which the application maintains its own local copy of the data to work with. Ideally this copy of the data is exactly same as the backend store. The cache ensures that the application gets the data whenever the application needs it at the faster rate. The access to the local copy of the data is much faster than fetching the data from the backend store. The caching is very robust and excellent feature provided by .NET framework. In this post, I will discuss the important concept called SQL dependency caching that can improve the performance of any business intelligence application drastically. There are numerous articles available on the internet which guides on the implementation of the SQL cache dependency in the application. I will restrict my blog post to include only the concept of SQL cache dependency from the perspective of framework's internal implementation. There are several other types of caching that are available in .NET framework which a developer can use in conjunction with each other.


The need of having the separate set of data can be well understood by the analogy of ATM - Automatic Teller Machine scenarios. The bank offers ATM service to reduce the overhead of managing customer's routine requests inside the bank premises. The customers can do their routine transactions in ATM very quickly instead of visiting the bank. The bank can reduce the number of customers rushing inside the bank every day by increasing number of ATM machines around it. Therefore the number of ATM machines are more than number of branches of the bank. The customer can always visit bank in case of failure of ATM machine. The ATM machine can be visualize as the cached data of the application. There can be several copies of the cache within the application depending on the type of operations performed by the feature of the application. The application can request the new copy of the data from the store in case of cache refreshes (just like periodic clearance, refilling and maintenance of the ATM machine done by the bank).


Let's understand few caching related concepts from .NET perspective.

  1. Cache: It is the in-memory store of data which reduces the database calls from the application
  2. Cache Dependency: The dependency is the actual store of data which resides on same or different machine. Dependency can be understood as a link between cache and actual store. The runtime clears the cached data when there are any changes in the dependency object
  3. Callbacks: These are the special types of functions that gets executed when there are changes in the actual store. They are known as callbacks because they are invoked by the operating system on the application when the dependency object changes. The callback happens real-time which means the callback function executes as soon as operating system detects the changes in the dependency object.

SQL Dependency Caching:

The SQL dependency caching works with the SQL server. It is the type of dependency for the application cache object. The SQL dependency enables MSSQL1 to notify the application when the data in the underlying tables changes. SQL cache dependency can be enabled on MSSQL by few configuration steps as provided below:


Configuration on SQL server:

  1. Enable caching infrastructure at the server level using aspnet_regsql command.

e.g.: aspnet_regsql -S dumbledore -U sa -P welcome@123 -d Northwind -ed

The above command creates following database objects for SQL server's internal use:

  1. Table: AspNet_SqlCacheTablesForChangeNotification: This table maintains the list of the tablenames that are registered for cache dependency. This means the SQL server notifies the application when data changes in any of these tables.
  2. SP2: AspNet_SqlCachePollingStoredProcedure: This stored procedure polls the database to know if the data has been changed from the last poll. This stored procedure is executed periodically within the time period set in config files. Sql profiler can trace the cal to this SP calls when configured for a database server.
  3. SP: AspNet_SqlCacheQueryRegisteredTablesStoredProcedure: This SP gets the list of the tables registered for SQL cache dependency.
  4. SP: AspNet_SqlCacheRegisterTableStoredProcedure: This SP registers the table for the sql cache dependency which means that the table is monitored and the application is notified if the underlying data changes
  1. SP: AspNet_SqlCacheUnRegisterTableStoredProcedure: This SP unregisters the table for the sql cache dependency which means that the table is no longer monitored for the dependency notifications
  1. SP: AspNet_SqlCacheUpdateChangeIdStoredProcedure: This SP updates the version number of the table in the AspNet_SqlCacheTablesForChangeNotification table after the data has been modified. This is used by the polling stored procedure to decide whether the data has been changed or not since the last poll.

    Figure: 1 - Enable sql dependency at database level

    Figure:2 - Tables created after enabling sql cache dependency on MSSQL

    Figure-3: Stored procedures created after enabling sql cache dependency on MSSQL

  1. Enable table for sql cache dependency using aspnet_regsql command.

Figure-4: Enable SQL cache dependency for a table in MSSQL

e.g.: aspnet_regsql -S dumbledore -U sa -P welcome@123 -d Northwind -t Categories -et

The above command creates the trigger on the table specified with -t argument (in this case, Categories). The trigger updates the version number in AspNet_SqlCacheTablesForChangeNotification table to ensure that the next poll recognizes the changes in this table.

Figure-5: Triggers created after enabling SQL cache dependency on table in MSSQL


  1. Enable the broker on the database using ALTER DATABASE NORTHWIND SET ENABLE_BROKER

This ensures that the broker service is set on the database which notifies all the applications that has registered for the SQL cache dependencies.


Application code:

    1. The application should register itself for SQL cache dependencies. This can be done using SqlDependency.Start(connectionString). This needs to be done at the application level.
    1. The application should create cache dependency objects using SqlCacheDependency. The constructor takes two arguments:
      1. databaseEntryName: Name of the database used in the config file
      1. tableName: Name of the table to set for sql cache dependency

    This needs to be done at the feature level. The dependency should include all the database tables which needs to be registered for the change notifications.

    1. The application should create the cache object using Cache.Add(...) and pass the SqlCacheDependency object (created in step 2) as the CacheDependency parameter
    1. We're good to go.

Validating the code:

  1. Execute the code without any changes in the database. The value should be picked up from the cache
  2. Execute the code after modifying the data in the database. The changes can happen from the application or SQL Management Studio. As soon as the change happens on database the operating system raises the callback to execute the event of the type CacheItemRemovedCallback delegate. The cache should be invalidated and application should make the new database call and refresh the cache

Various types of dependency:

The application can set the multiple dependencies on the same cache object using AggregateCacheDependency. e.g.: The application can register 5 database tables and 1 text document as the dependency for the single object. In this case, the application receives notification whenever anyone of the above dependency changes.

Applicability:

The caching improves the performance of the application drastically, if used with care. It works on the prediction of how the normal user will operate on the website. The caching is best suited while working with set of master data which does not change often. The application code may not fetch the same data everytime. Instead the code should read the data from the cache that are less likely to be updated frequently. We need to get the proper mix!




1MSSQL is acronym of Microsoft SQL Server
2SP is acronym of Stored Procedure