2023-07-16 15:00:07 -07:00
// Testing to make sure backend of library is operating properly.
# define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
# define GEN_ENFORCE_STRONG_CODE_TYPES
# define GEN_EXPOSE_BACKEND
# define GEN_BENCHMARK
2023-08-20 09:31:24 -07:00
# include "gen.hpp"
# include "gen.builder.hpp"
2023-07-16 15:00:07 -07:00
void check_sanity ( )
{
using namespace gen ;
gen : : init ( ) ;
log_fmt ( " \n check_sanity: \n " ) ;
// Test string caching:
CodeType t_int_dupe = def_type ( name ( int ) ) ;
if ( t_int_dupe - > Name ! = t_int - > Name )
2023-08-09 15:47:59 -07:00
GEN_FATAL ( " check_sanity: String caching failed! " ) ;
2023-07-16 15:00:07 -07:00
2023-08-09 06:50:12 -07:00
// Purposefully uses an excessive amount of memory to make sure the the memory backend doesn't break.
2023-07-16 15:00:07 -07:00
// This has been tested with num_iterations set to 15000000 (generates 15 million lines of code), the Global_BlockSize, along with CodePool_NumBlocks, and SizePer_StringArena
// must be adjusted to gigabytes(2), kilobytes(512), and gigabyte(1) for good performance without crashing.
/*
Typical usage ( megabytes ( 10 ) , kilobytes ( 4 ) , megabytes ( 1 ) , for 650000 ( the limit of 10 meg partition buckets in global arena ) )
2023-07-16 15:01:22 -07:00
Memory after builder :
Num Global Arenas : 14 TotalSize : 146800640 !
Num Code Pools : 794 TotalSize : 416284672 !
Num String Cache Arenas : 60 TotalSize : 62914560 !
Num String Cache : 1300007
2023-07-16 15:00:07 -07:00
Memory usage to expect at 15 mil file :
Num Global Arenas : 2 TotalSize : 4294967296 !
Num Code Pools : 144 TotalSize : 9663676416 !
Num String Cache Arenas : 2 TotalSize : 2147483648 !
Num String Cache : 30000025
*/
constexpr
2023-07-23 20:11:53 -07:00
s32 num_iterations = 325000 ;
2023-07-16 15:00:07 -07:00
2023-07-17 17:17:19 -07:00
Array < CodeTypedef > typedefs = Array < CodeTypedef > : : init_reserve ( GlobalAllocator , num_iterations * 2 ) ;
2023-07-16 15:00:07 -07:00
s32 idx = num_iterations ;
while ( - - idx )
{
// Stress testing string allocation
2023-07-17 17:17:19 -07:00
String type_name = String : : fmt_buf ( GlobalAllocator , " type_%ld " , idx ) ;
String typedef_name = String : : fmt_buf ( GlobalAllocator , " typedef_%ld " , idx ) ;
2023-07-16 15:00:07 -07:00
CodeTypedef type_as_int = def_typedef ( type_name , t_int ) ;
CodeType type = def_type ( type_name ) ;
CodeTypedef type_def = def_typedef ( typedef_name , type ) ;
typedefs . append ( type_as_int ) ;
typedefs . append ( type_def ) ;
}
log_fmt ( " \n Memory before builder: \n " ) ;
2023-07-17 17:17:19 -07:00
log_fmt ( " Num Global Arenas : %llu TotalSize: %llu ! \n " , Global_AllocatorBuckets . num ( ) , Global_AllocatorBuckets . num ( ) * Global_BucketSize ) ;
log_fmt ( " Num Code Pools : %llu TotalSize: %llu ! \n " , CodePools . num ( ) , CodePools . num ( ) * CodePool_NumBlocks * CodePools . back ( ) . BlockSize ) ;
log_fmt ( " Num String Cache Arenas : %llu TotalSize: %llu ! \n " , StringArenas . num ( ) , StringArenas . num ( ) * SizePer_StringArena ) ;
log_fmt ( " Num String Cache : %llu \n " , StringCache . Entries . num ( ) , StringCache ) ;
2023-07-16 15:00:07 -07:00
2023-08-20 12:45:06 -07:00
Builder builder = Builder : : open ( " ./gen/sanity.gen.hpp " ) ;
2023-07-16 15:00:07 -07:00
2023-07-17 17:17:19 -07:00
idx = typedefs . num ( ) ;
2023-07-16 15:00:07 -07:00
# ifdef GEN_BENCHMARK
2023-07-17 17:17:19 -07:00
u64 time_start = time_rel_ms ( ) ;
2023-07-16 15:00:07 -07:00
# endif
while ( - - idx )
{
builder . print ( typedefs [ idx ] ) ;
}
builder . write ( ) ;
# ifdef GEN_BENCHMARK
log_fmt ( " \n \n Builder finished writting. Time taken: %llu ms \n " , time_rel_ms ( ) - time_start ) ;
# endif
log_fmt ( " \n Memory after builder: \n " ) ;
2023-07-17 17:17:19 -07:00
log_fmt ( " Num Global Arenas : %llu TotalSize: %llu ! \n " , Global_AllocatorBuckets . num ( ) , Global_AllocatorBuckets . num ( ) * Global_BucketSize ) ;
log_fmt ( " Num Code Pools : %llu TotalSize: %llu ! \n " , CodePools . num ( ) , CodePools . num ( ) * CodePool_NumBlocks * CodePools . back ( ) . BlockSize ) ;
log_fmt ( " Num String Cache Arenas : %llu TotalSize: %llu ! \n " , StringArenas . num ( ) , StringArenas . num ( ) * SizePer_StringArena ) ;
log_fmt ( " Num String Cache : %llu \n " , StringCache . Entries . num ( ) , StringCache ) ;
2023-07-16 15:00:07 -07:00
gen : : deinit ( ) ;
2023-07-18 21:49:54 -07:00
log_fmt ( " \n Sanity passed! \n " ) ;
2023-07-16 15:00:07 -07:00
}