Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Featuring MemoryManager to resolve issues related to native memory leaking #613

Draft
wants to merge 44 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
fd3f19e
feat: Added new natives:
KillStr3aK Oct 6, 2024
6351158
feat: added `MemoryManager` related `CoreConfig` options
KillStr3aK Oct 6, 2024
421662a
feat: `MemoryManager` implementation
KillStr3aK Oct 6, 2024
50741aa
tweak: reordered
KillStr3aK Oct 6, 2024
c8b17af
fix: call `MemAlloc_Free` instead of `delete`
KillStr3aK Oct 6, 2024
5b4849e
tweak: now returns the correct value
KillStr3aK Oct 6, 2024
09a83a1
tweak: adjustments
KillStr3aK Oct 6, 2024
06998d9
feat: calculate GC speed
KillStr3aK Oct 6, 2024
42338ca
feat: sleep thread if 0 was released
KillStr3aK Oct 6, 2024
3b09a85
change `TotalReleased` type to `ulong`
KillStr3aK Oct 6, 2024
3b7f75a
feat: commands and adjustments
KillStr3aK Oct 6, 2024
c943cd2
tweak: add error message
KillStr3aK Oct 6, 2024
711319d
feat: allocation / total memory
KillStr3aK Oct 6, 2024
d3788d9
tweak: more log
KillStr3aK Oct 6, 2024
288c2ba
fix: remove state change
KillStr3aK Oct 6, 2024
47475b0
tweak: rename command to `css_memory`
KillStr3aK Oct 7, 2024
af2c922
feat: added `Resume`
KillStr3aK Oct 7, 2024
00eb021
tweak: this should be private
KillStr3aK Oct 7, 2024
8320d29
tweak: native for `MemAlloc_Free`
KillStr3aK Oct 7, 2024
a8ca731
tweak: adjust changes with the new native
KillStr3aK Oct 7, 2024
7190ea5
feat: native `MemAllocAllocate`
KillStr3aK Oct 7, 2024
f2250c6
fix: return void*
KillStr3aK Oct 7, 2024
83916a6
tweak: `DisposableMemory` is now abstract
KillStr3aK Oct 7, 2024
d390796
fix: stuck on `InProgress` state when it should be idle
KillStr3aK Oct 7, 2024
c1ba9a9
tweak: this is no longer needed
KillStr3aK Oct 7, 2024
2ce4ddd
feat: throw error for nullptr
KillStr3aK Oct 7, 2024
b7975d8
feat: `MemAlloc` class
KillStr3aK Oct 7, 2024
f253c66
feat: more memalloc natives
KillStr3aK Oct 7, 2024
c03e157
chore: fixed inconvenient name
KillStr3aK Oct 7, 2024
4fca199
chore: added todo marks
KillStr3aK Oct 7, 2024
edadc86
fix: dont release pure pointers
KillStr3aK Oct 8, 2024
8df7f05
tweak: cleanup and handle generics aswell
KillStr3aK Oct 8, 2024
ce27ee7
feat: these should be `DisposableMemory`
KillStr3aK Oct 8, 2024
6d1af88
feat: rework
KillStr3aK Oct 8, 2024
01eac8b
feat: `ForceCollect`
KillStr3aK Oct 9, 2024
e24f5f7
Merge branch 'main' into feature/memory-manager
KillStr3aK Oct 11, 2024
98fc755
Merge branch 'main' into feature/memory-manager
KillStr3aK Oct 13, 2024
c80b51c
Merge branch 'main' into feature/memory-manager
KillStr3aK Oct 19, 2024
78a8be8
Merge branch 'main' into feature/memory-manager
KillStr3aK Oct 21, 2024
e3d91a7
Merge branch 'main' into feature/memory-manager
KillStr3aK Nov 6, 2024
955e579
Merge branch 'main' into feature/memory-manager
KillStr3aK Nov 15, 2024
29c6ed9
Merge branch 'main' into feature/memory-manager
KillStr3aK Nov 26, 2024
e4a703c
Merge branch 'main' into feature/memory-manager
KillStr3aK Dec 3, 2024
37d8ded
Merge branch 'main' into feature/memory-manager
KillStr3aK Feb 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
"PluginAutoLoadEnabled": true,
"ServerLanguage": "en",
"UnlockConCommands": true,
"UnlockConVars": true
"UnlockConVars": true,
"EnableMemoryManager": true,
"MemoryManagerInterval": 10000
}
67 changes: 67 additions & 0 deletions managed/CounterStrikeSharp.API/Core/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,73 @@ public static void RemoveAllNetworkVectorElements(IntPtr vec){
}
}

public static IntPtr MemAllocAllocate(int size){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(size);
ScriptContext.GlobalScriptContext.SetIdentifier(0x9F81F710);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (IntPtr)ScriptContext.GlobalScriptContext.GetResult(typeof(IntPtr));
}
}

public static IntPtr MemAllocAllocateAligned(int size, int align){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(size);
ScriptContext.GlobalScriptContext.Push(align);
ScriptContext.GlobalScriptContext.SetIdentifier(0x7D6BFE63);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (IntPtr)ScriptContext.GlobalScriptContext.GetResult(typeof(IntPtr));
}
}

public static IntPtr MemAllocReallocateAligned(IntPtr pointer, int size, int align){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(pointer);
ScriptContext.GlobalScriptContext.Push(size);
ScriptContext.GlobalScriptContext.Push(align);
ScriptContext.GlobalScriptContext.SetIdentifier(0x3C513734);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (IntPtr)ScriptContext.GlobalScriptContext.GetResult(typeof(IntPtr));
}
}

public static int MemAllocGetSizeAligned(IntPtr pointer){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(pointer);
ScriptContext.GlobalScriptContext.SetIdentifier(0x814A9CB2);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (int)ScriptContext.GlobalScriptContext.GetResult(typeof(int));
}
}

public static void MemAllocFreePointer(IntPtr pointer){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(pointer);
ScriptContext.GlobalScriptContext.SetIdentifier(0xC2100D1D);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
}
}

public static void MemAllocFreePointerAligned(IntPtr pointer){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(pointer);
ScriptContext.GlobalScriptContext.SetIdentifier(0x88571A2E);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
}
}

public static short GetSchemaOffset(string classname, string propname){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
Expand Down
6 changes: 5 additions & 1 deletion managed/CounterStrikeSharp.API/Core/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Text;
using CounterStrikeSharp.API.Core.Commands;
using CounterStrikeSharp.API.Core.Hosting;
using CounterStrikeSharp.API.Core.Memory;
using CounterStrikeSharp.API.Core.Plugin;
using CounterStrikeSharp.API.Core.Plugin.Host;
using CounterStrikeSharp.API.Core.Translations;
Expand Down Expand Up @@ -48,11 +49,12 @@ public sealed class Application
private readonly IPluginContextQueryHandler _pluginContextQueryHandler;
private readonly IPlayerLanguageManager _playerLanguageManager;
private readonly ICommandManager _commandManager;
private readonly IMemoryManager _memoryManager;

public Application(ILoggerFactory loggerFactory, IScriptHostConfiguration scriptHostConfiguration,
GameDataProvider gameDataProvider, CoreConfig coreConfig, IPluginManager pluginManager,
IPluginContextQueryHandler pluginContextQueryHandler, IPlayerLanguageManager playerLanguageManager,
ICommandManager commandManager)
ICommandManager commandManager, IMemoryManager memoryManager)
{
Logger = loggerFactory.CreateLogger("Core");
_scriptHostConfiguration = scriptHostConfiguration;
Expand All @@ -62,6 +64,7 @@ public Application(ILoggerFactory loggerFactory, IScriptHostConfiguration script
_pluginContextQueryHandler = pluginContextQueryHandler;
_playerLanguageManager = playerLanguageManager;
_commandManager = commandManager;
_memoryManager = memoryManager;
_instance = this;
}

Expand Down Expand Up @@ -90,6 +93,7 @@ public void Start()
RegisterPluginCommands();

_pluginManager.Load();
_memoryManager.Load();

for (var i = 1; i <= 9; i++)
{
Expand Down
15 changes: 15 additions & 0 deletions managed/CounterStrikeSharp.API/Core/CoreConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ internal sealed partial class CoreConfigData

[JsonPropertyName("UnlockConVars")]
public bool UnlockConVars { get; set; } = true;

[JsonPropertyName("EnableMemoryManager")]
public bool EnableMemoryManager { get; set; } = true;

[JsonPropertyName("MemoryManagerInterval")]
public int MemoryManagerInterval { get; set; } = 10000;
}

/// <summary>
Expand Down Expand Up @@ -115,6 +121,15 @@ public partial class CoreConfig

public static bool UnlockConVars => _coreConfig.UnlockConVars;

/// <summary>
/// Enable the <b>EXPERIMENTAL</b> MemoryManager.
/// </summary>
public static bool EnableMemoryManager => _coreConfig.EnableMemoryManager;

/// <summary>
/// The interval (in milliseconds) at which the memory manager checks for leaking references.
/// </summary>
public static int MemoryManagerInterval => _coreConfig.MemoryManagerInterval;
}

public partial class CoreConfig : IStartupService
Expand Down
31 changes: 31 additions & 0 deletions managed/CounterStrikeSharp.API/Core/Memory/IMemoryManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This file is part of CounterStrikeSharp.
* CounterStrikeSharp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CounterStrikeSharp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
*/

namespace CounterStrikeSharp.API.Core.Memory
{
public interface IMemoryManager
{
public void Load();

public void Start();

public void Stop(bool forceStop = false);

public void Resume();

public void ForceCollect(int generation, GCCollectionMode mode, bool blocking);
}
}
Loading
Loading