﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Mircea Nita  / DBA Tools:sp_whocpu / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 21:15:34 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: DBA Tools:sp_whocpu</title><link>http://www.sqlservercentral.com/Forums/Topic895431-2662-1.aspx</link><description>Hello,You are deleting sleeping processes if the @loginame is in 'Active'. But what happens if I don't give any login name, which means @loginame would be null? Does the procedure still delete the sleeping processes from the temporary table?Thanks.</description><pubDate>Thu, 26 May 2011 09:12:03 GMT</pubDate><dc:creator>RimonTL</dc:creator></item><item><title>RE: DBA Tools:sp_whocpu</title><link>http://www.sqlservercentral.com/Forums/Topic895431-2662-1.aspx</link><description>Hi,Thank you very much for your comments and your suggestion.The stored procedure did have an interval parameter in the design phase, however, I found in the past two years since using it that 2 seconds is the minimum time to get meaningful figures, and from 4 or 5 seconds onwards the wait until you get results is too long.You can certainly use a parameter for the interval, however, this opens new possibilities for things to go astray, as many DBAs including me think of time in milliseconds, and it would be easy to pass by mistake 4000 instead of 4 as parameter if you have four seconds in mind. This would be less than ideal.The main use for this stored procedure is as a very fast tool to get the top processes consuming cpu, so although I am generally against hard coding, I decided not to use parameters - just call sp_whocpu and get the results.Thanks,Mircea</description><pubDate>Sat, 17 Apr 2010 18:41:43 GMT</pubDate><dc:creator>Mircea</dc:creator></item><item><title>RE: DBA Tools:sp_whocpu</title><link>http://www.sqlservercentral.com/Forums/Topic895431-2662-1.aspx</link><description>Love this script.  Hope you don't mind, I added one tweak, to pass a time parameter in (default to 3 seconds).  Changes bolded below.[code]if exists (select * from master.dbo.sysobjects where id = object_id('dbo.sp_whocpu') )  Drop Procedure dbo.sp_whocpugo/*====================================================================-- Mircea Anton Nita - 2010-- https://www.mcpvirtualbusinesscard.com/VBCServer/Mircea/card======================================================================*/Create Procedure dbo.sp_whocpu   [b]@samplingTime int = 3, -- seconds to pass, but maybe don't get carried away[/b]   @dbname sysname = null,   @loginame sysname = nullasset nocount ondeclare   @retcode int  ,@sidlow varbinary(85)  ,@sidhigh varbinary(85)  ,@sid1 varbinary(85)  ,@spidlow int  ,@spidhigh int  ,@seldbid varchar(10)  ,@charMaxLenLoginName varchar(24)  ,@charMaxLenDBName varchar(24)  ,@charMaxLenCPUTime varchar(10)  ,@charMaxLenCPUDelta varchar(10)  ,@charMaxLenDiskIO varchar(10)  ,@charMaxLenHostName varchar(24)  ,@charMaxLenProgramName varchar(10)  ,@charMaxLenLastBatch varchar(10)  ,@charMaxLenCommand varchar(10)  ,@charsidlow varchar(85)  ,@charsidhigh varchar(85)  ,@charspidlow varchar(11)  ,@charspidhigh varchar(11)  ,@command varchar(8000)  ,[b]@samplingTimeString varchar(10) -- string version of sampleTime to pass to WAITFOR DELAY[/b][b]-- CHANGES TO TAKE VARIABLE SAMPLING TIMEif @samplingTime &amp;lt; 3 set @samplingTime = 3 -- use the acceptable minimumSELECT @samplingTimeString  = cast( (    CASE WHEN @samplingTime/3600&amp;lt;10 THEN '0' ELSE '' END     + RTRIM(@samplingTime/3600)     + ':' + RIGHT('0'+RTRIM((@samplingTime % 3600) / 60),2)     + ':' + RIGHT('0'+RTRIM((@samplingTime % 3600) % 60),2) ) as varchar(10))[/b]-- set defaultsset @retcode   = 0set @sidlow    = convert(varbinary(85), (replicate(char(0), 85)))set @sidhigh   = convert(varbinary(85), (replicate(char(1), 85)))set @spidlow   = 0set @spidhigh  = 32767if (@dbname is not null)  set @seldbid = cast((select top 1 dbid from master.dbo.sysdatabases where name like '%'+@dbname+'%') as varchar(10))else  set @seldbid = '0'if (@loginame is null) -- Simple default to all LoginNames.  GOTO LABEL_PARAMselect @sid1 = nullif exists(select * from sys.syslogins where loginname = @loginame)  select @sid1 = sid from sys.syslogins where loginname = @loginameif (@sid1 is not null) -- The parameter is a recognized login name.  begin  select @sidlow = suser_sid(@loginame)        ,@sidhigh = suser_sid(@loginame)  GOTO LABEL_PARAM  endif (lower(@loginame collate Latin1_General_CI_AS) in ('Active')) -- Special action, not sleeping.  begin  select @loginame = lower(@loginame collate Latin1_General_CI_AS)  GOTO LABEL_PARAM  endif (patindex ('%[^0-9]%' , isnull(@loginame,'z')) = 0) -- Is a number.  begin  select     @spidlow = convert(int, @loginame)    ,@spidhigh = convert(int, @loginame)  GOTO LABEL_PARAMendraiserror(15007,-1,-1,@loginame)select @retcode = 1GOTO LABEL_RETURNLABEL_PARAM:-- Getting data over a time window to allow the cpu_delta metric calculationif object_id('tempdb.dbo.#cpu1') is not null drop table #cpu1if object_id('tempdb.dbo.#cpu2') is not null drop table #cpu2select spid, cpu into #cpu1 from master.dbo.sysprocesses with (nolock) order by cpu descwaitfor delay @sampling_time_stringselect spid, cpu into #cpu2 from master.dbo.sysprocesses with (nolock) order by cpu desc-------------------- Capture consistent sysprocesses. -------------------select   sp.spid  ,status  ,sid  ,hostname  ,program_name  ,cmd  ,sp.cpu  ,c2.cpu-c1.cpu as 'cpu_delta'  ,physical_io  ,blocked  ,dbid  ,convert(sysname, rtrim(loginame)) as loginname  ,sp.spid as 'spid_sort'  , substring( convert(varchar,last_batch,111) ,6 ,5 ) + ' '  + substring( convert(varchar,last_batch,113) ,13 ,8 ) as 'last_batch_char'    into #tb1_sysprocesses    from #cpu2 c2 join #cpu1 c1 on c2.spid = c1.spid join master.dbo.sysprocesses sp with (nolock) on sp.spid = c2.spid    where c2.cpu-c1.cpu &amp;gt; 0 if @@error &amp;lt;&amp;gt; 0   begin     select @retcode = @@error     GOTO LABEL_RETURN   endif (@loginame in ('active'))  delete #tb1_sysprocesses    where lower(status) = 'sleeping'    and upper(cmd) in (        'AWAITING COMMAND'        ,'LAZY WRITER'        ,'CHECKPOINT SLEEP'        )    and blocked = 0    and dbid &amp;lt;&amp;gt; @seldbid-- Prepare to dynamically optimize column widths.select   @charsidlow = convert(varchar(85),@sidlow)  ,@charsidhigh = convert(varchar(85),@sidhigh)  ,@charspidlow = convert(varchar,@spidlow)  ,@charspidhigh = convert(varchar,@spidhigh)select   @charMaxLenLoginName =    convert( varchar    ,isnull( max( datalength(loginname)) ,16)    )  ,@charMaxLenDBName =    convert( varchar    ,isnull( max( datalength( rtrim(convert(varchar(128),db_name(dbid))))) ,20)    )  ,@charMaxLenCPUTime =    convert( varchar    ,isnull( max( datalength( rtrim(convert(varchar(128),cpu)))) ,10)    )  ,@charMaxLenCPUDelta =    convert( varchar    ,isnull( max( datalength( rtrim(convert(varchar(128),cpu_delta)))) ,10)    )  ,@charMaxLenDiskIO =    convert( varchar  ,isnull( max( datalength( rtrim(convert(varchar(128),physical_io)))) ,6)    )  ,@charMaxLenCommand =    convert( varchar    ,isnull( max( datalength( rtrim(convert(varchar(128),cmd)))) ,7)    )  ,@charMaxLenHostName =    convert( varchar    ,isnull( max( datalength( rtrim(convert(varchar(128),hostname)))) ,16)    )  ,@charMaxLenProgramName =    convert( varchar    ,isnull( max( datalength( rtrim(convert(varchar(128),program_name)))) ,11)    )  ,@charMaxLenLastBatch =    convert( varchar    ,isnull( max( datalength( rtrim(convert(varchar(128),last_batch_char)))) ,9)    )  from    #tb1_sysprocesses  where      spid &amp;gt;= @spidlow  and spid &amp;lt;= @spidhigh-- Output the report.set @command = 'set nocount offselect  SPID = convert(char(5),spid)  ,Status =    CASE lower(status)      When ''sleeping'' Then lower(status)      Else upper(status)    END  ,Login = substring(loginname,1,' + @charMaxLenLoginName + ')  ,HostName =    CASE hostname      When Null Then '' .''      When '' '' Then '' .''      Else substring(hostname,1,' + @charMaxLenHostName + ')    END  ,BlkBy =    CASE isnull(convert(char(5),blocked),''0'')      When ''0'' Then '' .''      Else isnull(convert(char(5),blocked),''0'')    END  ,DBName = substring(case when dbid = 0 then null when dbid &amp;lt;&amp;gt; 0 then db_name(dbid) end,1,' + @charMaxLenDBName + ')  ,Command = substring(cmd,1,' + @charMaxLenCommand + ')  ,CPU_Total = substring(convert(varchar,cpu),1,' + @charMaxLenCPUTime + ')  ,CPU_Delta = substring(convert(varchar,cpu_delta),1,' + @charMaxLenCPUDelta + ')  ,DiskIO = substring(convert(varchar,physical_io),1,' + @charMaxLenDiskIO + ')  ,LastBatch = substring(last_batch_char,1,' + @charMaxLenLastBatch + ')  ,ProgramName = substring(program_name,1,' + @charMaxLenProgramName + ')  ,SPID = convert(char(5),spid) -- Handy extra for right-scrolling users.from    #tb1_sysprocesseswhere spid &amp;gt; 50 -- filter out system spidsand spid &amp;lt;&amp;gt; @@spid -- and current process spidand spid &amp;gt;= ' + @charspidlow + 'and spid &amp;lt;= ' + @charspidhigh + ''if @seldbid &amp;gt; 0  set @command = @command +'  and dbid = ' + @seldbid + ''  set @command = @command +' order by cast(cpu_delta as int) desc, cast(cpu as int) descset nocount on'  exec (@command)LABEL_RETURN:if object_id('tempdb.dbo.#tb1_sysprocesses') is not null drop table #tb1_sysprocessesif object_id('tempdb.dbo.#cpu1') is not null drop table #cpu1if object_id('tempdb.dbo.#cpu2') is not null drop table #cpu2return @retcode -- sp_whocpugoif exists (select * from sysobjects   where id = object_id('dbo.sp_whocpu')   and sysstat &amp; 0xf = 4)  grant exec on dbo.sp_whocpu to publicgo[/code]</description><pubDate>Thu, 15 Apr 2010 11:29:12 GMT</pubDate><dc:creator>GabyYYZ</dc:creator></item><item><title>RE: DBA Tools:sp_whocpu</title><link>http://www.sqlservercentral.com/Forums/Topic895431-2662-1.aspx</link><description>Outstanding. This makes perfect sense as it was on my DEV server which had open connections but may not have been processing anything. :-)Thanks!</description><pubDate>Thu, 15 Apr 2010 07:36:47 GMT</pubDate><dc:creator>hugericc</dc:creator></item><item><title>RE: DBA Tools:sp_whocpu</title><link>http://www.sqlservercentral.com/Forums/Topic895431-2662-1.aspx</link><description>Hi,This is possible, if there is no activity on the server, during the 3 seconds period when the sp_whocpu calculates the cpu_delta.The spid that runs sp_whocpu is ignored.None of the sleeping processes which do not consume cpu will show either, so it is possible that at a certain moment you don't get any output.This is what differentiates sp_whocpu from sp_who2 for example. Running sp_who2 will show all processes, some with quite high cpu figures, when in fact they do not consume any resources at that moment in time.Cheers,Mircea</description><pubDate>Wed, 14 Apr 2010 19:37:02 GMT</pubDate><dc:creator>Mircea</dc:creator></item><item><title>RE: DBA Tools:sp_whocpu</title><link>http://www.sqlservercentral.com/Forums/Topic895431-2662-1.aspx</link><description>Thanks for the script.I've sucessfully run it and created the sp but when I run the sp itself in SS management studio (2005), I only get the colunm headers but no results.Am I missing something?</description><pubDate>Wed, 14 Apr 2010 08:29:27 GMT</pubDate><dc:creator>hugericc</dc:creator></item><item><title>DBA Tools:sp_whocpu</title><link>http://www.sqlservercentral.com/Forums/Topic895431-2662-1.aspx</link><description>Comments posted to this topic are about the item [b][url=/scripts/sp_whoCPU/69907/]DBA Tools: sp_whoCPU[/url][/b]Hi All,Just a note to let you know that the issue with the TAB characters has been resolved, so you can just copy and paste the script if you would like to use it.Thanks</description><pubDate>Thu, 01 Apr 2010 19:22:36 GMT</pubDate><dc:creator>Mircea</dc:creator></item></channel></rss>