Add best Memory cache usage info
This commit is contained in:
@@ -153,6 +153,14 @@ EndModule
|
||||
DeclareModule Cache
|
||||
EnableExplicit
|
||||
|
||||
; Structure to hold cache statistics / Structure pour contenir les statistiques du cache <--- MODIFIED
|
||||
Structure CacheStats
|
||||
TotalItems.i ; Total items in cache (loaded or not) / Nombre total d'éléments (chargés ou non)
|
||||
LoadedInMemory.i ; Number of images currently in memory / Nombre d'images actuellement en mémoire
|
||||
WaitingToLoad.i ; Number of images in the loading queue / Nombre d'images dans la file d'attente
|
||||
MemoryUsed.q ; Total memory used by loaded images in bytes / Mémoire totale utilisée par les images chargées en octets
|
||||
EndStructure
|
||||
|
||||
; A prototype for a callback function to load media. Allows custom loading logic. / Prototype pour une fonction de callback pour charger un média. Permet une logique de chargement personnalisée.
|
||||
Prototype.i CallBackLoadMedia(*Ptr.Core::FileData)
|
||||
|
||||
@@ -169,6 +177,7 @@ DeclareModule Cache
|
||||
Signal.b ; A flag that is set to True when an image has been loaded, to notify the main thread. / Un indicateur mis à Vrai quand une image a été chargée, pour notifier le thread principal.
|
||||
QuitMutex.i ; Mutex to protect access to the Quit flag. / Mutex pour protéger l'accès à l'indicateur Quit.
|
||||
Quit.b ; A flag to signal all worker threads to terminate. / Un indicateur pour signaler à tous les threads de travail de se terminer.
|
||||
TotalMemoryUsed.q ; Running total of memory used by images / Total de la mémoire utilisée par les images
|
||||
EndStructure
|
||||
|
||||
Global Param.Param ; Global instance of the parameter structure. / Instance globale de la structure de paramètres.
|
||||
@@ -180,6 +189,8 @@ DeclareModule Cache
|
||||
Declare.i GetFileDataFromCache(FilePath.s, Image.i = 0) ; Retrieves file data from the cache, or queues it for loading if not present. / Récupère les données d'un fichier depuis le cache, ou le met en file d'attente de chargement s'il n'est pas présent.
|
||||
Declare.b GetSignalAndReset() ; Checks if a new image is ready and resets the signal. / Vérifie si une nouvelle image est prête et réinitialise le signal.
|
||||
Declare QuitCache() ; Properly shuts down the cache system and all worker threads. / Arrête proprement le système de cache et tous les threads de travail.
|
||||
Declare GetStats(*Stats.CacheStats) ; Get current cache statistics / Récupère les statistiques actuelles du cache
|
||||
|
||||
EndDeclareModule
|
||||
|
||||
Module Cache
|
||||
@@ -195,6 +206,7 @@ Module Cache
|
||||
Param\SignalMutex = CreateMutex()
|
||||
Param\QuitMutex = CreateMutex()
|
||||
Param\NewTaskSemaphore = CreateSemaphore(0)
|
||||
Param\TotalMemoryUsed = 0 ; Initialize memory counter / Initialise le compteur mémoire
|
||||
Dim Param\WorkerThreads(#WORKER_THREADS - 1)
|
||||
EndProcedure
|
||||
|
||||
@@ -249,6 +261,11 @@ Module Cache
|
||||
ResizeImage(*Ptr\Image, result\Width, result\Height, #PB_Image_Smooth)
|
||||
*Ptr\State = 1 ; State = 1 means "Loaded". / État = 1 signifie "Chargé".
|
||||
|
||||
; Securely add the new image's memory size to the total / Ajoute de manière sécurisée la taille mémoire de la nouvelle image au total
|
||||
LockMutex(Param\CacheListMutex)
|
||||
Param\TotalMemoryUsed + (result\Width * result\Height * (ImageDepth(*Ptr\Image) / 8))
|
||||
UnlockMutex(Param\CacheListMutex)
|
||||
|
||||
; Safely set the signal to notify the main thread that a new image is ready for display. / Met à jour de manière sécurisée le signal pour notifier le thread principal qu'une nouvelle image est prête à être affichée.
|
||||
LockMutex(Param\SignalMutex)
|
||||
Param\Signal = #True
|
||||
@@ -407,6 +424,8 @@ Module Cache
|
||||
; Instead of destroying the structure, we only free the image / Au lieu de détruire la structure, on libère seulement l'image
|
||||
; and reset the state. The pointer remains valid. / et on réinitialise l'état. Le pointeur reste valide.
|
||||
If IsImage(*Ptr\Image)
|
||||
; Subtract the image's memory size from the total before freeing it / Soustrait la taille mémoire de l'image du total avant de la libérer
|
||||
Param\TotalMemoryUsed - (ImageWidth(*Ptr\Image) * ImageHeight(*Ptr\Image) * (ImageDepth(*Ptr\Image) / 8))
|
||||
FreeImage(*Ptr\Image)
|
||||
*Ptr\Image = 0
|
||||
*Ptr\State = 0 ; State = "Not Loaded" / État = "Non chargé"
|
||||
@@ -418,6 +437,29 @@ Module Cache
|
||||
Next
|
||||
UnlockMutex(Param\CacheListMutex)
|
||||
EndProcedure
|
||||
|
||||
; GetStats - Collects and returns the current statistics of the cache. / Collecte et retourne les statistiques actuelles du cache.
|
||||
Procedure GetStats(*Stats.CacheStats)
|
||||
If *Stats
|
||||
LockMutex(Param\CacheListMutex)
|
||||
*Stats\TotalItems = MapSize(Param\CacheList())
|
||||
*Stats\MemoryUsed = Param\TotalMemoryUsed ; Read the pre-calculated total / Lit le total pré-calculé
|
||||
|
||||
Protected LoadedCount.i = 0
|
||||
ForEach Param\CacheList()
|
||||
If Param\CacheList()\State > 0
|
||||
LoadedCount + 1
|
||||
EndIf
|
||||
Next
|
||||
*Stats\LoadedInMemory = LoadedCount
|
||||
UnlockMutex(Param\CacheListMutex)
|
||||
|
||||
LockMutex(Param\LoadListMutex)
|
||||
*Stats\WaitingToLoad = ListSize(Param\LoadList())
|
||||
UnlockMutex(Param\LoadListMutex)
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
EndModule
|
||||
|
||||
; =================================================
|
||||
@@ -1071,6 +1113,20 @@ EndModule
|
||||
; =================================================
|
||||
CompilerIf #PB_Compiler_IsMainFile
|
||||
|
||||
; Procedure: FormatBytes - Converts a byte count into a human-readable string (KB, MB, GB) / Convertit un nombre d'octets en une chaîne lisible (Ko, Mo, Go).
|
||||
Procedure.s FormatBytes(bytes.q)
|
||||
If bytes >= 1024 * 1024 * 1024 ; GB / Go
|
||||
ProcedureReturn StrD(bytes / (1024.0 * 1024.0 * 1024.0), 2) + " GB"
|
||||
ElseIf bytes >= 1024 * 1024 ; MB / Mo
|
||||
ProcedureReturn StrD(bytes / (1024.0 * 1024.0), 2) + " MB"
|
||||
ElseIf bytes >= 1024 ; KB / Ko
|
||||
ProcedureReturn StrD(bytes / 1024.0, 2) + " KB"
|
||||
Else ; Bytes / Octets
|
||||
ProcedureReturn Str(bytes) + " B"
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Enumeration for the main window and gadgets. / Énumération pour la fenêtre principale et les gadgets.
|
||||
Enumeration
|
||||
#Win_main
|
||||
@@ -1079,10 +1135,11 @@ CompilerIf #PB_Compiler_IsMainFile
|
||||
#Gdt_ThumbA
|
||||
#Gdt_ThumbB
|
||||
#Gdt_ThumbSize
|
||||
#Gdt_FrameStyle ; <--- Ajoutez
|
||||
#Gdt_OptionFit ; <--- Ajoutez
|
||||
#Gdt_OptionFill ; <--- Ajoutez
|
||||
#Gdt_OptionStretch ; <--- Ajoutez
|
||||
#Gdt_FrameStyle
|
||||
#Gdt_OptionFit
|
||||
#Gdt_OptionFill
|
||||
#Gdt_OptionStretch
|
||||
#StatusBar_Main
|
||||
EndEnumeration
|
||||
|
||||
Global NewList CurrentList.s() ; A global list to hold the file paths of the images to be displayed. / Une liste globale pour contenir les chemins des fichiers des images à afficher.
|
||||
@@ -1144,6 +1201,11 @@ CompilerIf #PB_Compiler_IsMainFile
|
||||
Cache::InitCache() ; Initialize the caching system. / Initialise le système de cache.
|
||||
; Create the thumbnail gadget, passing our callback function. / Crée le gadget de vignettes, en passant notre fonction de callback.
|
||||
Thumbs::ThumbsGadget(#Gdt_ThumbA, 0, 50, WindowWidth(#Win_main), WindowHeight(#Win_main) - 50, 128, @CallBackLoadFromIndexB())
|
||||
If CreateStatusBar(#StatusBar_Main, WindowID(#Win_main))
|
||||
AddStatusBarField(400)
|
||||
EndIf
|
||||
; On ajoute un timer qui se déclenchera toutes les 500ms (2 fois par seconde)
|
||||
AddWindowTimer(#Win_main, 1, 500)
|
||||
|
||||
Repeat
|
||||
Event = WaitWindowEvent()
|
||||
@@ -1199,6 +1261,18 @@ CompilerIf #PB_Compiler_IsMainFile
|
||||
ResizeGadget(#Gdt_ThumbA, 0, 50, WindowWidth(#Win_main), WindowHeight(#Win_main) - 50)
|
||||
EndIf
|
||||
|
||||
If Event = #PB_Event_Timer And EventTimer() = 1
|
||||
Define Stats.Cache::CacheStats
|
||||
Cache::GetStats(@Stats)
|
||||
|
||||
; Format the text to display, now including memory usage
|
||||
; Formate le texte à afficher, en incluant maintenant l'utilisation mémoire
|
||||
Define StatText.s = "Cache Items: " + Stats\TotalItems + " | In Memory: " + Stats\LoadedInMemory + " | Queue: " + Stats\WaitingToLoad
|
||||
StatText + " | Mem. Used: " + FormatBytes(Stats\MemoryUsed)
|
||||
|
||||
StatusBarText(#StatusBar_Main, 0, StatText)
|
||||
EndIf
|
||||
|
||||
Until Event = #PB_Event_CloseWindow
|
||||
|
||||
; Clean up all resources before exiting. / Nettoie toutes les ressources avant de quitter.
|
||||
@@ -1214,9 +1288,9 @@ CompilerEndIf
|
||||
; EnableXP
|
||||
; DPIAware
|
||||
; IDE Options = PureBasic 6.21 (Windows - x64)
|
||||
; CursorPosition = 1192
|
||||
; FirstLine = 1149
|
||||
; Folding = -------
|
||||
; CursorPosition = 441
|
||||
; FirstLine = 434
|
||||
; Folding = --------
|
||||
; EnableThread
|
||||
; EnableXP
|
||||
; DPIAware
|
Reference in New Issue
Block a user