//-----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.
//
//-----------------------------------------------------------------------
namespace Microsoft.Isam.Esent.Interop.Vista
{
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
///
/// Contains cumulative statistics on the work performed by the database
/// engine on the current thread. This information is returned via
/// JetGetThreadStats.
///
[StructLayout(LayoutKind.Sequential)]
[SuppressMessage(
"Microsoft.StyleCop.CSharp.NamingRules",
"SA1300:ElementMustBeginWithUpperCaseLetter",
Justification = "This should match the unmanaged API, which isn't capitalized.")]
[Serializable]
[SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules",
"SA1305:FieldNamesMustNotUseHungarianNotation",
Justification = "This should match the unmanaged API, which isn't capitalized.")]
public struct JET_THREADSTATS : IEquatable
{
///
/// The size of a JET_THREADSTATS structure.
///
internal static readonly int Size = Marshal.SizeOf(typeof(JET_THREADSTATS));
///
/// Size of the structure. This is used for interop.
///
private readonly int cbStruct;
///
/// Number of pages visited.
///
private int pagesReferenced;
///
/// Number of pages read from disk.
///
private int pagesRead;
///
/// Number of pages preread.
///
private int pagesPreread;
///
/// Number of pages dirtied.
///
private int pagesDirtied;
///
/// Pages redirtied.
///
private int pagesRedirtied;
///
/// Number of log records generated.
///
private int numLogRecords;
///
/// Number of bytes logged.
///
private int loggedBytes;
///
/// Gets the total number of database pages visited by the database
/// engine on the current thread.
///
public int cPageReferenced
{
[DebuggerStepThrough]
get { return this.pagesReferenced; }
internal set { this.pagesReferenced = value; }
}
///
/// Gets the total number of database pages fetched from disk by the
/// database engine on the current thread.
///
public int cPageRead
{
[DebuggerStepThrough]
get { return this.pagesRead; }
internal set { this.pagesRead = value; }
}
///
/// Gets the total number of database pages prefetched from disk by
/// the database engine on the current thread.
///
public int cPagePreread
{
[DebuggerStepThrough]
get { return this.pagesPreread; }
internal set { this.pagesPreread = value; }
}
///
/// Gets the total number of database pages, with no unwritten changes,
/// that have been modified by the database engine on the current thread.
///
public int cPageDirtied
{
[DebuggerStepThrough]
get { return this.pagesDirtied; }
internal set { this.pagesDirtied = value; }
}
///
/// Gets the total number of database pages, with unwritten changes, that
/// have been modified by the database engine on the current thread.
///
public int cPageRedirtied
{
[DebuggerStepThrough]
get { return this.pagesRedirtied; }
internal set { this.pagesRedirtied = value; }
}
///
/// Gets the total number of transaction log records that have been
/// generated by the database engine on the current thread.
///
public int cLogRecord
{
[DebuggerStepThrough]
get { return this.numLogRecords; }
internal set { this.numLogRecords = value; }
}
///
/// Gets the total size, in bytes, of transaction log records that
/// have been generated by the database engine on the current thread.
///
public int cbLogRecord
{
[DebuggerStepThrough]
get { return this.loggedBytes; }
internal set { this.loggedBytes = value; }
}
///
/// Create a new struct with the specified
/// valued.
///
///
/// Number of pages visited.
///
///
/// Number of pages read.
///
///
/// Number of pages preread.
///
///
/// TNumber of pages dirtied.
///
///
/// Number of pages redirtied.
///
///
/// Number of log records generated.
///
///
/// Bytes of log records written.
///
///
/// A new struct with the specified values.
///
public static JET_THREADSTATS Create(
int cPageReferenced,
int cPageRead,
int cPagePreread,
int cPageDirtied,
int cPageRedirtied,
int cLogRecord,
int cbLogRecord)
{
return new JET_THREADSTATS
{
cPageReferenced = cPageReferenced,
cPageRead = cPageRead,
cPagePreread = cPagePreread,
cPageDirtied = cPageDirtied,
cPageRedirtied = cPageRedirtied,
cLogRecord = cLogRecord,
cbLogRecord = cbLogRecord,
};
}
///
/// Add the stats in two JET_THREADSTATS structures.
///
/// The first JET_THREADSTATS.
/// The second JET_THREADSTATS.
/// A JET_THREADSTATS containing the result of adding the stats in t1 and t2.
public static JET_THREADSTATS Add(JET_THREADSTATS t1, JET_THREADSTATS t2)
{
unchecked
{
return new JET_THREADSTATS
{
cPageReferenced = t1.cPageReferenced + t2.cPageReferenced,
cPageRead = t1.cPageRead + t2.cPageRead,
cPagePreread = t1.cPagePreread + t2.cPagePreread,
cPageDirtied = t1.cPageDirtied + t2.cPageDirtied,
cPageRedirtied = t1.cPageRedirtied + t2.cPageRedirtied,
cLogRecord = t1.cLogRecord + t2.cLogRecord,
cbLogRecord = t1.cbLogRecord + t2.cbLogRecord,
};
}
}
///
/// Add the stats in two JET_THREADSTATS structures.
///
/// The first JET_THREADSTATS.
/// The second JET_THREADSTATS.
/// A JET_THREADSTATS containing the result of adding the stats in t1 and t2.
public static JET_THREADSTATS operator +(JET_THREADSTATS t1, JET_THREADSTATS t2)
{
return Add(t1, t2);
}
///
/// Calculate the difference in stats between two JET_THREADSTATS structures.
///
/// The first JET_THREADSTATS.
/// The second JET_THREADSTATS.
/// A JET_THREADSTATS containing the difference in stats between t1 and t2.
public static JET_THREADSTATS Subtract(JET_THREADSTATS t1, JET_THREADSTATS t2)
{
unchecked
{
return new JET_THREADSTATS
{
cPageReferenced = t1.cPageReferenced - t2.cPageReferenced,
cPageRead = t1.cPageRead - t2.cPageRead,
cPagePreread = t1.cPagePreread - t2.cPagePreread,
cPageDirtied = t1.cPageDirtied - t2.cPageDirtied,
cPageRedirtied = t1.cPageRedirtied - t2.cPageRedirtied,
cLogRecord = t1.cLogRecord - t2.cLogRecord,
cbLogRecord = t1.cbLogRecord - t2.cbLogRecord,
};
}
}
///
/// Calculate the difference in stats between two JET_THREADSTATS structures.
///
/// The first JET_THREADSTATS.
/// The second JET_THREADSTATS.
/// A JET_THREADSTATS containing the difference in stats between t1 and t2.
public static JET_THREADSTATS operator -(JET_THREADSTATS t1, JET_THREADSTATS t2)
{
return Subtract(t1, t2);
}
///
/// Determines whether two specified instances of JET_THREADSTATS
/// are equal.
///
/// The first instance to compare.
/// The second instance to compare.
/// True if the two instances are equal.
public static bool operator ==(JET_THREADSTATS lhs, JET_THREADSTATS rhs)
{
return lhs.Equals(rhs);
}
///
/// Determines whether two specified instances of JET_THREADSTATS
/// are not equal.
///
/// The first instance to compare.
/// The second instance to compare.
/// True if the two instances are not equal.
public static bool operator !=(JET_THREADSTATS lhs, JET_THREADSTATS rhs)
{
return !(lhs == rhs);
}
///
/// Gets a string representation of this object.
///
/// A string representation of this object.
public override string ToString()
{
// String.Concat is faster than using a StringBuilder.
// use Int32.ToString instead of passing the Int32 to
// String.Format (which requires boxing).
return string.Concat(
this.cPageReferenced.ToString("N0", CultureInfo.InvariantCulture),
" page reference",
GetPluralS(this.cPageReferenced),
", ",
this.cPageRead.ToString("N0", CultureInfo.InvariantCulture),
" page",
GetPluralS(this.cPageRead),
" read, ",
this.cPagePreread.ToString("N0", CultureInfo.InvariantCulture),
" page",
GetPluralS(this.cPagePreread),
" preread, ",
this.cPageDirtied.ToString("N0", CultureInfo.InvariantCulture),
" page",
GetPluralS(this.cPageDirtied),
" dirtied, ",
this.cPageRedirtied.ToString("N0", CultureInfo.InvariantCulture),
" page",
GetPluralS(this.cPageRedirtied),
" redirtied, ",
this.cLogRecord.ToString("N0", CultureInfo.InvariantCulture),
" log record",
GetPluralS(this.cLogRecord),
", ",
this.cbLogRecord.ToString("N0", CultureInfo.InvariantCulture),
" byte",
GetPluralS(this.cbLogRecord),
" logged");
}
///
/// Returns a value indicating whether this instance is equal
/// to another instance.
///
/// An object to compare with this instance.
/// True if the two instances are equal.
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return this.Equals((JET_THREADSTATS)obj);
}
///
/// Returns the hash code for this instance.
///
/// The hash code for this instance.
public override int GetHashCode()
{
return this.cPageReferenced
^ this.cPageRead << 1
^ this.cPagePreread << 2
^ this.cPageDirtied << 3
^ this.cPageRedirtied << 4
^ this.cLogRecord << 5
^ this.cbLogRecord << 6;
}
///
/// Returns a value indicating whether this instance is equal
/// to another instance.
///
/// An instance to compare with this instance.
/// True if the two instances are equal.
public bool Equals(JET_THREADSTATS other)
{
return this.cbLogRecord == other.cbLogRecord
&& this.cLogRecord == other.cLogRecord
&& this.cPageDirtied == other.cPageDirtied
&& this.cPagePreread == other.cPagePreread
&& this.cPageRead == other.cPageRead
&& this.cPageRedirtied == other.cPageRedirtied
&& this.cPageReferenced == other.cPageReferenced;
}
///
/// Get the plural suffix ('s') for the given number.
///
/// The number.
/// The letter 's' if n is greater than 1.
private static string GetPluralS(int n)
{
return n == 1 ? string.Empty : "s";
}
}
}