for Developers

Daniel Moth Developer and Platform Group http://www.danielmoth.com/Blog AGENDA

Top 7 Ways To “Light Up” Your Apps on Windows 2008 Part 1 emphasis on IIS7, PowerShell Part 2 emphasis on WER, Restart and Recovery , TxF 1. Build More Flexible Web Apps 2. Design Highly-Manageable Apps 3. Develop Federation-Aware Apps 4. Build Connected Systems The Top 7 Ways… Part 2

1. Build More Flexible Web Applications 2. Design Highly-Manageable Applications 3. Develop Federation-Aware Applications 4. Build Connected Systems 5. Build For Scalability 6. Develop More Reliable Applications 7. Virtualize 5. Build For Scalability Native Threading Enhancements in and

Thread Pools

One-Time Initialization

Slim Reader/Writer Lock

Condition Variables

Thread Ordering Service

Wait Chain Traversal Thread Pool in Vista and Server 2008

Re-architected Thread Pool Simpler, more reliable, higher performance Does not use a timer thread Single queue Dedicated persistent thread Clean-up groups Single worker thread type (both I/O and non-I/O) Multiple pools per process More flexible API Feature Original API Current API CloseThreadpoolWait RegisterWaitForSingleObject CreateThreadpoolWait Synch UnregisterWaitEx SetThreadpoolWait WaitForThreadpoolWaitCallbacks CloseThreadpoolWork CreateThreadpoolWork QueueUserWorkItem SubmitThreadpoolWork Work TrySubmitThreadpoolCallback WaitForThreadpoolWorkCallbacks CreateTimerQueue CloseThreadpoolTimer CreateTimerQueueTimer CreateThreadpoolTimer ChangeTimerQueueTimer IsThreadpoolTimerSet Timer DeleteTimerQueueTimer SetThreadpoolTimer DeleteTimerQueueEx WaitForThreadpoolTimerCallbacks CancelThreadpoolIo CloseThreadpoolIo BindIoCompletionCallback CreateThreadpoolIo I/O StartThreadpoolIo WaitForThreadpoolIoCallbacks CloseThreadpoolCleanupGroup CloseThreadpoolCleanupGroupMembers Clean-up group CreateThreadpoolCleanupGroup CloseThreadpool CreateThreadpool Pool SetThreadpoolThreadMaximum SetThreadpoolThreadMinimum DestroyThreadpoolEnvironment InitializeThreadpoolEnvironment SetThreadpoolCallbackCleanupGroup Callback environment SetThreadpoolCallbackLibrary SetThreadpoolCallbackPool SetThreadpoolCallbackRunsLong Callback CallbackMayRunLong DisassociateCurrentThreadFromCallback FreeLibraryWhenCallbackReturns LeaveCriticalSectionWhenCallbackReturns Callback clean up ReleaseMutexWhenCallbackReturns ReleaseSemaphoreWhenCallbackReturns SetEventWhenCallbackReturns One-Time Initialization

Interlocked functions ensure that only one thread performs the initialization One-time initialization is better Optimized for speed Appropriate barriers are created on processor architectures that require them Support for both locked and parallel initialization No internal locking so the code can operate asynchronously or synchronously One Time Init Steps BOOL WINAPI InitOnceBeginInitialize( __inout LPINIT_ONCE lpInitOnce, __in DWORD dwFlags, __out PBOOL fPending, __out LPVOID* lpContext ); BOOL WINAPI InitOnceExecuteOnce( __inout PINIT_ONCE InitOnce, __in PINIT_ONCE_FN InitFn, __inout_opt PVOID Parameter, __out LPVOID* Context ); BOOL WINAPI InitOnceComplete( __inout LPINIT_ONCE lpInitOnce, __in DWORD dwFlags, __in LPVOID lpContext ); BOOL CALLBACK InitOnceCallback( __inout PINIT_ONCE InitOnce, __inout_opt PVOID Parameter, __out_opt PVOID* Context ); Slim Reader/Writer Lock SRW locks – new synchronization primitive enable threads to access shared resources optimized for speed take very little memory built on top of windows kernel keyed events Two modes Shared mode – grants shared read-only access to multiple reader threads Exclusive mode – grants read/write access to one writer thread at a time SRW Lock APIs

VOID WINAPI InitializeSRWLock( __out PSRWLOCK SRWLock ); VOID WINAPI AcquireSRWLockExclusive( __inout PSRWLOCK SRWLock ); VOID WINAPI ReleaseSRWLockExclusive( __inout PSRWLOCK SRWLock ); VOID WINAPI AcquireSRWLockShared( __inout PSRWLOCK SRWLock ); VOID WINAPI ReleaseSRWLockShared( __inout PSRWLOCK SRWLock ); Condition Variables

Used to synchronize a group of threads based on the result of some conditional test Enable threads to atomically release a lock and enter the sleeping state Benefits Much clearer and less error-prone Can be more efficient – Tries to avoid trips to kernel mode (unlike WaitForSingleObject) Condition Variable APIs

VOID WINAPI InitializeConditionVariable( __out PCONDITION_VARIABLE ConditionVariable ); BOOL WINAPI SleepConditionVariableCS( __inout PCONDITION_VARIABLE ConditionVariable, __inout PCRITICAL_SECTION CriticalSection, __in DWORD dwMilliseconds ); BOOL WINAPI SleepConditionVariableSRW( __inout PCONDITION_VARIABLE ConditionVariable, __inout PSRWLOCK SRWLock, __in DWORD dwMilliseconds, __in ULONG Flags ); VOID WINAPI WakeConditionVariable( __inout PCONDITION_VARIABLE ConditionVariable ); VOID WINAPI WakeAllConditionVariable( __inout PCONDITION_VARIABLE ConditionVariable ); Thread Ordering Service (TOS)

TOS controls the execution of client threads Ensures that they run once and in order 5 APIs – AvRtXxxxThreadOrderingGroup parent thread calls Create to set up the TOS client threads call Join to join the TOS all of them call Wait, run their code and Wait ...client threads call Leave when they are done parent thread calls Delete to end it all TOS APIs - avrt.h BOOL WINAPI AvRtCreateThreadOrderingGroup( __out PHANDLE Context, __in PLARGE_INTEGER Period, __inout GUID* ThreadOrderingGuid, __in_opt PLARGE_INTEGER Timeout );

BOOL WINAPI AvRtJoinThreadOrderingGroup( __out PHANDLE Context, __in GUID* ThreadOrderingGuid, __in BOOL Before );

BOOL WINAPI AvRtWaitOnThreadOrderingGroup( __in HANDLE Context );

BOOL WINAPI AvRtLeaveThreadOrderingGroup( __in HANDLE Context );

BOOL WINAPI AvRtDeleteThreadOrderingGroup( __in HANDLE Context ); Wait Chain Traversal (WCT)

Enables debuggers to diagnose application hangs and deadlocks

“Wait chain is an alternating sequence of threads and synchronization objects; each thread waits for the object that follows it, which is owned by the subsequent thread in the chain”

WCT supports the following ALPC, COM, Critical sections, Mutexes, SendMessage WCT APIs in Wct.h

HWCT WINAPI OpenThreadWaitChainSession( __in DWORD Flags, __in_opt PWAITCHAINCALLBACK callback );

BOOL WINAPI GetThreadWaitChain( __in HWCT WctHandle, __in_opt DWORD_PTR Context, __in DWORD Flags, __in DWORD ThreadId, __inout LPDWORD NodeCount, __out PWAITCHAIN_NODE_INFO NodeInfoArray, __out LPBOOL IsCycle );

VOID WINAPI CloseThreadWaitChainSession( __in HWCT WctHandle );

VOID CALLBACK WaitChainCallback( HWCT WctHandle, DWORD_PTR Context, DWORD CallbackStatus, LPDWORD NodeCount, PWAITCHAIN_NODE_INFO NodeInfoArray, LPBOOL IsCycle );

typedef struct _WAITCHAIN_NODE_INFO { Native Threading Enhancements in Windows Vista and Windows Server 2008

Thread Pools One-Time Initialization Slim Reader/Writer Lock Condition Variables Thread Ordering Service Wait Chain Traversal

MSDN Magazine: Oct07, Jun07, Jul07 6. Develop More Reliable Apps & winqual

New User Experience In addition to crashes, hangs are also detected Privacy evaluation, Queuing and transport Problem Reports and Solutions Response management New Public APIs Adding additional file and memory data to a report (inc. minidump & heap information) Create reports for custom events WER, Restart, Recovery

Restart API

Register to be restarted after fatal problems Registration also used for Restart Manager – Restarts process after patch installation All applications should support restart – Especially if support document recovery How it works Register command-line that should be called every execution – HRESULT RegisterApplicationRestart (IN PCWSTR pwzCommandline, DWORD dwFlags) After fatal event is reported, app is restarted – Fatal events block user tasks – Automatically restarting saves users from having to re-open the application Recovery APIs

Attempt to recover data after a fatal event Users should not lose any work to an app bug How it works 1. App registers a “recovery callback” every execution HRESULT RegisterApplicationRecoveryCallback (IN RECOVERY_ROUTINE RecoveryRoutine, IN PVOID pvParameter) 2. Recovery routine called after data collection – Application’s code attempts to recover user work – Flush to disk, repair on next run of application – Repair data in memory, save to disk – Need to call RecoveryInProgress() every 5 seconds to heartbeat – Call RecoveryFinished() to signal recovery is completed Restart Manager Overview

With the Restart Manager technology installers can Automatically shutdown only the applications and services holding a file to be updated When available, leverage specific application functionality to restore the user to the state they were in before the restart When a reboot cannot be avoided, automatically re-launch apps after reboot

Windows Installer (MSI) Key Microsoft Office 2007 Applications

• Fewer reboots when using the Windows • Automatically restart after a reboot due Add/Remove Programs feature to an installation or update

• Developers can reduce reboots for • Recreate application state upon restart installations and updates by using the v4.0 Restart Manager Restart Manager, Call to Action

Installer software call the Restart Manager APIs Applications and Services Restart Manager "aware” Transactional NTFS

Windows Vista and Windows Server 2008

Does what it says on the tin

System.Transactions.dll + PInvoke Transactional Platform Kernel Transaction Manager (KTM) Makes transactions available as kernel objects Provides transaction management services to system components such as TxF Can communicate with DTC to enable distributed transactions Transactional NTFS (TxF) Integrates transactions directly into the NTFS Transactional Registry (TxR) Integrates transactions directly into the Registry Transactional NTFS (TxF)

System.Tx LTM Managed SQL DTC

Native KtmRm KtmW32 MSMQ

WCF KTM NTFS Registry WS-* Kernel CLFS TxF 7. Virtualize Hosted Virtualization Products Microsoft Virtual PC 2007 Over 3.5 million downloads Support for Windows Vista as a host and guest 64-bit host support Improved performance Support for Intel VT and AMD-V Microsoft Virtual Server 2005 R2 SP1 Support for Intel VT and AMD-V Support for SLES 10 VSS integration for live backup of running virtual machines VHD mounting tool for offline servicing Improved performance Improved scalability: 512 VMs on x64 systems Microsoft Virtualization Products A comprehensive set of virtualization products, from the data center to the desktop Assets – both virtual and physical – are managed from a single platform

Server Virtualization

Hyper-V Server Presentation Virtualization

Management

Application Desktop Virtualization Virtualization

Centralized Desktop Virtualization Benefits Server consolidation Utilization

Business Continuity Flexibility Hyper-V Codename "Viridian", Windows Server Virtualization Flexible and dynamic virtualization solution A role of Windows Server 2008 (Std, EE, DC) Can be a full role with local UI or role Hypervisor based architecture Managed by Microsoft System Center Also provided as a standalone server Microsoft Hyper-V Server ($28) Windows Server Core role Server Core Minimal Installation option in all x86/x64 editions Command line interface only, no GUI shell Provides essential server functionality Deployed for a single role – No need to deploy and service the whole OS Benefits Less code results in fewer patches and servicing burden Low surface area targeted for server roles More secure and reliable with less management Virtualization and High Availability

Traditional Non-Virtualized Virtualized Environment Environment Downtime is bad, but affects Value of the physical server goes up only one workload Downtime is far worse because multiple workloads are affected

Virtualization and High-Availability Go Hand in Hand Microsoft Hyper-V High Availability

Planned downtime More common than unplanned Quickly move virtualized workloads in order to service underlying hardware

Unplanned downtime Not as common and more difficult Automatic failover to other nodes – hardware or power failure Quick Migration Fundamentals Planned Downtime

Save state VHDs Save entire virtual Shared Storage machine state Move virtual machine Move storage connectivity from origin to destination host Restore state and Run Network Connectivity Restore virtual machine and run Quick Migration – Planned Downtime

Domain Virtualization Servers • Active server requires Controller (3 + 1 Servers) servicing • Move virtualized workloads to a standby server • ~4 seconds downtime per virtual machine

System Center Virtual Machine Manager

Windows Server 2008 Failover Cluster Manager

Ethernet Storage Connectivity VHDs on SAN Quick Migration – Unplanned Downtime

Domain Virtualization Servers • Active server loses power (3 + 1 Servers) Controller • Virtual machines automatically restart on the next cluster node • If there is not enough memory, the failover automatically moves to the System Center next node until done Virtual Machine Manager

Windows Server 2008 Failover Cluster Manager

Ethernet Storage Connectivity VHDs on SAN Quick Migration – How Quick Is It?

Disc / 1 GbE iSCSI 2 Gb FC 4 Gb FC VM Memory

512 MB ~8 seconds ~ 4 seconds ~2 seconds

~ 4 seconds 1 GB ~16 seconds ~8 second

~8 seconds 2 GB ~32 seconds ~16 seconds

~16 seconds 4 GB ~64 seconds ~32 seconds

8 GB ~2 minutes ~64 seconds ~32 seconds Terminology Hypervisor A piece of software that provides the ability to run multiple operating systems on one piece of hardware Ensures that the CPU and hardware answer the *correct* OS Microkernelized or Monolithic Hyper-V A role you can install in Windows that includes the Hypervisor as well as management software Partition An “” to the hypervisor Virtual Machine A “child” partition Hyper-V Overview

Management tools

Virtualization VM 1 VM 2 VM 2 Platform and “Parent” “Child” “Child” Management

Windows Hypervisor Powerful virtualization built into the Windows platform Hyper-V Architecture

Parent Partition Child Partitions Provided by:

Virtualization Stack Windows Applications WMI Provider Hyper-V VM Worker VM Processes ISV Service User Mode OEM

Virtualization Service Windows Server Core Virtualization Clients Kernel Service (VSCs) Windows Providers Kernel IHV Drivers (VSPs) VMBus Enlightenments Kernel Mode

Windows hypervisor

“Designed for Windows” Server Hardware Example VSP/VSC Design

Parent Partition Child Partitions Provided by:

VM Worker Process Applications Windows User Mode Hyper-V Windows File System ISV Volume OEM Partition Virtual Storage Disk Provider (VSP) Disk Fast Path Filter (VSC)

StorPort Virtual Storage iSCSIprt StorPort Miniport (VSC) Miniport VMBus Kernel Mode

Windows hypervisor Hardware Monolithic vs. Microkernelized

Monolithic hypervisor Microkernelized hypervisor Simpler than a modern kernel, Simple partitioning functionality but still complex Increase reliability and minimize TCB Contains its own drivers model No third-party code Drivers run within guests VM 1 VM 2 VM 3 VM 1 (“Admin”) (“Parent”) Virtual- VM 2 VM 3 ization (“Child”) (“Child”) Stack

Hypervisor DriversDriversDrivers DriversDriversDrivers DriversDriversDrivers

DriversDriversDrivers Hypervisor

Hardware Hardware

VMware ESX Approach Hyper-V Approach

Microkernelized Hypervisor has an inherently secure architecture with minimal attack surface Virtual Server 2005 vs. Hyper-V

Virtual Server 2005 R2 SP1 Hyper-V

32-bit Virtual Machines Yes Yes

64-bit Virtual Machines No Yes

Multi Processor Virtual Machines No Yes, up to 4 core VMs

Virtual Machine Memory Support 3.6GB per VM 32GB per VM

Physical Memory Support 256 GB 1 TB Managed by System Center Virtual Yes Yes Machine Manager Support for Microsoft Clustering Yes Yes Services Host side backup support (VSS) Yes Yes

Scriptable / Extensible COM WMI + HyperCall API

User Interface Web Interface MMC 3.0 Interface Hyper-V Features and Abilities Performance Hypervisor Synthetic Drivers Server Core Flexibility Multi-architecture Multi-OS VM’s Manageability Managed through WMI (PowerShell) SCVMM Windows Interoperability & Standards VHD (Virtual Hard Disk) VHD specification is freely available under Open Specification Promise (OSP) VHD TestDrive program for ISVs Standards based management APIs DMTF defining industry standard model for VM management Hyper-V uses this model Hypervisor hypercall API Preliminary documentation available under OSP Final version will be at RTM Interoperablity & 3rd Party OS Support Linux Working with XenSource – Developing adapter layer to map Xen hypercall API to Hyper-V hypercall API – Developing disk and networking drivers (VSCs) to integrate with the new I/O architecture Working with Novell – Interoperability and joint support for Windows Server and Novel SUSE Linux Enterprise Server 10 Support for Linux on Hyper-V Solaris Working with Sun to support Solaris on Hyper-V Industry Leadership In Licensing

Instance Based Licensing Will enable new usage models

Windows Server Enterprise/Datacenter Includes 4/Unlimited virtual instances

Licensing per Virtual Processor SQL Server, BizTalk Server, etc.

Windows Vista (Software Assurance Customer Benefit) Allows Vista Enterprise Centralized Desktop deployments

Demo Distribution of Virtual Images Virtualization Investments

Infrastructure Interoperability Management Licensing Applications

Create agility Support Ease Deliver Accelerate Better utilize heterogeneity consolidation cost- deployment server across the onto virtual effective, Reduce the resources datacenter infrastructure flexible and cost of OSP (Open Better utilize simplified supporting Partner with licensing AMD and Specification management applications Intel Promise) resources Turn apps VHD and Free up IT into dynamic, HyperCall spend real-time services Summary

Build More Flexible Web Applications Design Highly-Manageable Applications Develop Federation-Aware Applications Build Connected Systems Build For Scalability Develop More Reliable Applications Virtualize MSDN in the UK

Visit http://msdn.co.uk Newsletter Events Screencasts Blogs © 2007 Microsoft Ltd. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.