mirror of
https://github.com/ppy/osu.git
synced 2025-02-15 10:22:56 +08:00
Add database statistics to GlobalStatistics (#5208)
Add database statistics to GlobalStatistics
This commit is contained in:
commit
51fe8c3ab6
@ -5,6 +5,7 @@ using System.Linq;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Microsoft.EntityFrameworkCore.Storage;
|
using Microsoft.EntityFrameworkCore.Storage;
|
||||||
using osu.Framework.Platform;
|
using osu.Framework.Platform;
|
||||||
|
using osu.Framework.Statistics;
|
||||||
|
|
||||||
namespace osu.Game.Database
|
namespace osu.Game.Database
|
||||||
{
|
{
|
||||||
@ -31,11 +32,20 @@ namespace osu.Game.Database
|
|||||||
recycleThreadContexts();
|
recycleThreadContexts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static readonly GlobalStatistic<int> reads = GlobalStatistics.Get<int>("Database", "Get (Read)");
|
||||||
|
private static readonly GlobalStatistic<int> writes = GlobalStatistics.Get<int>("Database", "Get (Write)");
|
||||||
|
private static readonly GlobalStatistic<int> commits = GlobalStatistics.Get<int>("Database", "Commits");
|
||||||
|
private static readonly GlobalStatistic<int> rollbacks = GlobalStatistics.Get<int>("Database", "Rollbacks");
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get a context for the current thread for read-only usage.
|
/// Get a context for the current thread for read-only usage.
|
||||||
/// If a <see cref="DatabaseWriteUsage"/> is in progress, the existing write-safe context will be returned.
|
/// If a <see cref="DatabaseWriteUsage"/> is in progress, the existing write-safe context will be returned.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public OsuDbContext Get() => threadContexts.Value;
|
public OsuDbContext Get()
|
||||||
|
{
|
||||||
|
reads.Value++;
|
||||||
|
return threadContexts.Value;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Request a context for write usage. Can be consumed in a nested fashion (and will return the same underlying context).
|
/// Request a context for write usage. Can be consumed in a nested fashion (and will return the same underlying context).
|
||||||
@ -45,6 +55,7 @@ namespace osu.Game.Database
|
|||||||
/// <returns>A usage containing a usable context.</returns>
|
/// <returns>A usage containing a usable context.</returns>
|
||||||
public DatabaseWriteUsage GetForWrite(bool withTransaction = true)
|
public DatabaseWriteUsage GetForWrite(bool withTransaction = true)
|
||||||
{
|
{
|
||||||
|
writes.Value++;
|
||||||
Monitor.Enter(writeLock);
|
Monitor.Enter(writeLock);
|
||||||
OsuDbContext context;
|
OsuDbContext context;
|
||||||
|
|
||||||
@ -90,9 +101,15 @@ namespace osu.Game.Database
|
|||||||
if (usages == 0)
|
if (usages == 0)
|
||||||
{
|
{
|
||||||
if (currentWriteDidError)
|
if (currentWriteDidError)
|
||||||
|
{
|
||||||
|
rollbacks.Value++;
|
||||||
currentWriteTransaction?.Rollback();
|
currentWriteTransaction?.Rollback();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
commits.Value++;
|
||||||
currentWriteTransaction?.Commit();
|
currentWriteTransaction?.Commit();
|
||||||
|
}
|
||||||
|
|
||||||
if (currentWriteDidWrite || currentWriteDidError)
|
if (currentWriteDidWrite || currentWriteDidError)
|
||||||
{
|
{
|
||||||
|
@ -6,6 +6,7 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using osu.Framework.Logging;
|
using osu.Framework.Logging;
|
||||||
|
using osu.Framework.Statistics;
|
||||||
using osu.Game.Beatmaps;
|
using osu.Game.Beatmaps;
|
||||||
using osu.Game.Configuration;
|
using osu.Game.Configuration;
|
||||||
using osu.Game.IO;
|
using osu.Game.IO;
|
||||||
@ -34,6 +35,8 @@ namespace osu.Game.Database
|
|||||||
|
|
||||||
private static readonly Lazy<OsuDbLoggerFactory> logger = new Lazy<OsuDbLoggerFactory>(() => new OsuDbLoggerFactory());
|
private static readonly Lazy<OsuDbLoggerFactory> logger = new Lazy<OsuDbLoggerFactory>(() => new OsuDbLoggerFactory());
|
||||||
|
|
||||||
|
private static readonly GlobalStatistic<int> contexts = GlobalStatistics.Get<int>("Database", "Contexts");
|
||||||
|
|
||||||
static OsuDbContext()
|
static OsuDbContext()
|
||||||
{
|
{
|
||||||
// required to initialise native SQLite libraries on some platforms.
|
// required to initialise native SQLite libraries on some platforms.
|
||||||
@ -76,6 +79,8 @@ namespace osu.Game.Database
|
|||||||
connection.Close();
|
connection.Close();
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
contexts.Value++;
|
||||||
}
|
}
|
||||||
|
|
||||||
~OsuDbContext()
|
~OsuDbContext()
|
||||||
@ -85,6 +90,20 @@ namespace osu.Game.Database
|
|||||||
Dispose();
|
Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool isDisposed;
|
||||||
|
|
||||||
|
public override void Dispose()
|
||||||
|
{
|
||||||
|
if (isDisposed) return;
|
||||||
|
|
||||||
|
isDisposed = true;
|
||||||
|
|
||||||
|
base.Dispose();
|
||||||
|
|
||||||
|
contexts.Value--;
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
{
|
{
|
||||||
base.OnConfiguring(optionsBuilder);
|
base.OnConfiguring(optionsBuilder);
|
||||||
|
Loading…
Reference in New Issue
Block a user