Friday 20 September 2013

Quick HTML Template

This is the template that I can copy-paste whenever I need some simple HTML page. You are free to use it for yourself.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title></title>

<!-- <link rel="stylesheet" type="text/css" href="" /> -->
<!--<script type="text/javascript" src=""></script>-->
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>

<style type="text/css">
body { background-color: #adadad; font-size:8pt; }
</style>
<script type="text/javascript">
//<![CDATA[

//]]>
</script>
</head>

<body>

<div id="main"></div>

<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;">
</canvas>

<script type="text/javascript">
//<![CDATA[

$(document).ready(function(){
});

/* for handling canvas events, if needed */
$(window).load(function(){
});

//]]>
</script>
</body>

</html>

Wednesday 18 September 2013

Query to show Performance Counters that SQL Server exposes to Windows OS

T-SQL query that can quickly show some valuable performance counters from SQL Server. The values in this system view are actual, they get refreshed many times a second.

SELECT *
FROM master.sys.dm_os_performance_counters
WHERE
instance_name = 'MyDbName'
OR OBJECT_NAME = 'SQLServer:General Statistics'

OR counter_name = 'Batch Requests/sec'

Troubleshooting: if this query returns nothing then you may have Performance Counters disabled. Run this to verify, it should return at least one row : SELECT COUNT(*) from sys.dm_os_performance_counters;


Friday 14 December 2012

SQL Server, query performance and Execution Plan panel



Noticed one interesting thing with sql management studio.
Was trying to pinpoint some performance issues with a query which had WHILE loop. I switched on the execution plan to analyse the query. It was strange – like if performance was degrading with each new iteration. After several rounds of optimizations there was no luck, same performance degradation.

I created very simple test shown below (Example 1). It does nothing but counts the rows in sys.objects table 500 times so it returns the same result each time. There are 1570 rows in  MyDatabase.sys.objects on my machine

Still no luck – average execution times were rising, total time was more than a minute

Switched off the execution plan and it complete in less than a second!

The conclusion is that Execution Plan panel should be used carefully when your queries have WHILE loops – use Profiler or use SET SHOWPLAN_ALL ON or SET SHOWPLAN_XML ON statements as alternative (see Example 2)

--EXAMPLE1:

USE MyDatabase

SET STATISTICS TIME OFF;
set nocount on;

DECLARE @indexRow INT = 0, @cnt int

SET STATISTICS TIME ON
WHILE @indexRow <= 500
BEGIN
       SELECT @cnt=COUNT(*), @indexRow = @indexRow + 1 FROM sys.objects
END          
SET STATISTICS TIME OFF

--EXAMPLE2:

SET SHOWPLAN_ALL ON;
GO

DECLARE @indexRow INT = 0, @cnt int

WHILE @indexRow <= 500
BEGIN
       SELECT @cnt=COUNT(*), @indexRow = @indexRow + 1 FROM sys.objects
END
GO

SET SHOWPLAN_ALL OFF;
GO

Monday 30 July 2012

Resize the partition on Windows Server for free and non-destructive

Problem: after cloning smaller hard drive (HDD, 80 Gb) to a larger solid-state drive (SSD, 128 Gb), the partitions on the SSD became exactly the same as on HDD. 40 Gb were "lost". I needed a way to expand the partition on the SSD not loosing the files on it. Did it on Windows Server 2008 x64, with an idea to install that SSD in the laptop where HDD belonged.

Tried these free utilities:
Paragon Partition Manager Free Edition - doesn't work with server OS. Rumours are it also doesn't support dynamic disks
MiniTool Partition Wizard Home Edition 7.5 - doesn't support Windows Server OS.

Solution:



Sunday 29 July 2012

Updating firmware on OCZ Vertex 4 SSD

After several unsuccessfull attempts to update the firmware on my new SSD - OCZ Vertex 4 - I think I found a way that works.

Monday 25 June 2012

Enabling CAT.Net to work with Visual Studio 2010

Microsoft recommends using their CAT.Net Security Code Analysis tool as part of the SDL (Software Development Process). Unfortunately, latest released version is CAT.Net v1 CTP. There was CAT.Net 2.0 Beta but it disappeared even from Microsoft's sites due to some incompatibility or missing libraries (where??).

What is left of CAT.Net 2.0 for the community is this video on Channel 9.

CAT.Net 1.0 doesn't work with Visual Studio 2010 unless you manually alter (hack) its config file a little. See below.

  1. Close Visual Studio 2010 IDE
  2. Find this file: %APPDATA%\Microsoft\MSEnvShared\Addins\Microsoft.ACESec.CATNet.AddIn
  3. Edit it in your preferred text editor, adding the line <Version>10.0</Version> right after <Version>9.0</Version>
  4. Open IDE again. Go to Tools->CAT.Net Code Analysis

Hope it helps.

P.S.: there is some information on how to use CAT.Net and OWASP O2 Platform along with Roslyn compiler outside of the IDE. It is all on Dinis Cruz blog.

Update 1: Found a blog post from April 2011 which says this:
At this point in time we are accepting recommendations, suggestions and new features.  However, we do not have any planned updates for the remainder of the fiscal year.  We are going through our FY12 planning and CAT.NET is on the list of requests for next year.  We will know by the end of June if funding has be approved.  At that time we’ll notify people of the budgetary decisions. 

Thursday 21 June 2012

Alternative to sp_changedbowner which is being deprecated

SQL Server Books online suggest that will is being deprecated starting from 2008 (not even 2008 R2) and will be removed from future releases of SQL Server.
What is the alternative?
Meet much more flexible command - ALTER AUTHORIZATION - which is available from version 2005 onwards.

so instead of


USE MyDatabase
GO
EXEC sp_changedbowner 'sa'
GO


you may do


USE MyDatabase
GO
ALTER AUTHORIZATION ON DATABASE::MyDatabase TO sa;
GO




Wednesday 13 June 2012

wix, managed code custom action may fail when temp folder is compressed

msi installer was created by WiX 3.5. VM with Windows Server 2003 SP1 32 bit, Windows Installer 4.5, .Net 3.5 SP1.

It had a c# custom action named Managed_PutFeatureState. That custom action was failing with not many clues in the log (see below). Added logging to c# code, added try\catch constructs to prevent any possible failures and it still was failing. Noticed that it fails maybe 80% of times which was even more annoying.

I thought that it had something to do with files it creates in %TEMP% folder, by some reason I suspected that it either couldn't unpack or couldn't find an unpacked .dll file with the custom action itself.

Ran ProcMon.exe from sysinternals to see what is going on on the disk. And it stopped failing when ProcMon was capturing the events!

This started me thinking about disk i\o. Played with various virtualization settings, host i\o enabled\disabled, etc to no joy. Still it was failing.

Solution: disable "compressed" attribute on %TEMP% folder!

ran via command line with logging like this:
msiexec /l*vx! install.log /i MySetup.msi 
The log:

MSI (c) (50:54) [13:02:15:952]: Doing action: Managed_PutFeatureState
Action 13:02:15: Managed_PutFeatureState.
Action start 13:02:15: Managed_PutFeatureState.
MSI (c) (50:54) [13:02:16:046]: Creating MSIHANDLE (5) of type 790542 for thread 3924
MSI (c) (50:E8) [13:02:16:046]: Invoking remote custom action. DLL: C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\MSI9.tmp, Entrypoint: PutFeatureState
MSI (c) (50!F0) [13:02:16:108]: Creating MSIHANDLE (6) of type 790531 for thread 4080
MSI (c) (50!F0) [13:02:16:124]: Closing MSIHANDLE (6) of type 790531 for thread 4080
MSI (c) (50!F0) [13:02:16:186]: Creating MSIHANDLE (7) of type 790531 for thread 4080
MSI (c) (50!F0) [13:02:16:186]: Closing MSIHANDLE (7) of type 790531 for thread 4080
MSI (c) (50!F0) [13:02:16:327]: Creating MSIHANDLE (8) of type 790531 for thread 4080
MSI (c) (50!F0) [13:02:16:343]: Closing MSIHANDLE (8) of type 790531 for thread 4080
MSI (c) (50!F0) [13:02:16:374]: Note: 1: 2727 2:
MSI (c) (50!F0) [13:02:16:389]: Note: 1: 2205 2:  3: MsiAssembly
MSI (c) (50!F0) [13:02:16:389]: Note: 1: 2228 2:  3: MsiAssembly 4:  SELECT `MsiAssembly`.`Attributes`, `MsiAssembly`.`File_Application`, `MsiAssembly`.`File_Manifest`,  `Component`.`KeyPath` FROM `MsiAssembly`, `Component` WHERE  `MsiAssembly`.`Component_` = `Component`.`Component` AND `MsiAssembly`.`Component_` = ?
MSI (c) (50!F0) [13:02:16:389]: Note: 1: 1402 2: HKEY_CLASSES_ROOT\.4 3: 2
MSI (c) (50!F0) [13:02:16:405]: Note: 1: 2205 2:  3: _RemoveFilePath
MSI (c) (50!F0) [13:02:16:405]: Note: 1: 2727 2:
MSI (c) (50!F0) [13:02:16:514]: Note: 1: 2727 2:
MSI (c) (50!F0) [13:02:16:546]: Note: 1: 2727 2:
MSI (c) (50:E8) [13:02:16:577]: Closing MSIHANDLE (5) of type 790542 for thread 3924
Action ended 13:02:16: Managed_PutFeatureState. Return value 3.
DEBUG: Error 2896:  Executing action Managed_PutFeatureState failed.
The installer has encountered an unexpected error installing this package. This may indicate a problem with this package. The error code is 2896. The arguments are: Managed_PutFeatureState, ,
Action ended 13:02:16: WelcomeDlg. Return value 3.
MSI (c) (50:58) [13:02:16:608]: Doing action: FatalError
Action 13:02:16: FatalError.
Action start 13:02:16: FatalError.
Action 13:02:16: FatalError. Dialog created
Action ended 13:02:17: FatalError. Return value 2.
Action ended 13:02:17: INSTALL. Return value 3.
MSI (c) (50:58) [13:02:17:686]: Destroying RemoteAPI object.
MSI (c) (50:9C) [13:02:17:686]: Custom Action Manager thread ending.