Finalize configures the memory calculator in the startup command
()
| 161 | |
| 162 | // Finalize configures the memory calculator in the startup command |
| 163 | func (m *MemoryCalculator) Finalize() error { |
| 164 | m.LoadConfig() |
| 165 | |
| 166 | // If calculatorPath not set, try to detect it from previous installation |
| 167 | if m.calculatorPath == "" { |
| 168 | m.detectInstalledCalculator() |
| 169 | } |
| 170 | |
| 171 | if m.calculatorPath == "" { |
| 172 | return nil // Not installed |
| 173 | } |
| 174 | |
| 175 | m.ctx.Log.Info("Configuring Memory Calculator") |
| 176 | |
| 177 | // The memory calculator command will be added to the startup script |
| 178 | // It's executed at runtime to calculate memory based on actual container limits |
| 179 | // Format: CALCULATED_MEMORY=$(calculator args) && JAVA_OPTS="$JAVA_OPTS $CALCULATED_MEMORY" |
| 180 | |
| 181 | // We'll write this to a shell script that containers can source |
| 182 | memoryCalcScript := filepath.Join(m.ctx.Stager.DepDir(), "bin", "memory_calculator.sh") |
| 183 | if err := os.MkdirAll(filepath.Dir(memoryCalcScript), 0755); err != nil { |
| 184 | return fmt.Errorf("failed to create bin directory: %w", err) |
| 185 | } |
| 186 | |
| 187 | // Build calculator command (v4.x format) |
| 188 | calculatorCmd := m.buildCalculatorCommand() |
| 189 | |
| 190 | scriptContent := fmt.Sprintf(`#!/bin/bash |
| 191 | # Memory Calculator - calculates optimal JVM memory settings |
| 192 | if [ -n "$MEMORY_LIMIT" ]; then |
| 193 | CALCULATED_MEMORY=$(%s) |
| 194 | echo "JVM Memory Configuration: $CALCULATED_MEMORY" |
| 195 | export JAVA_OPTS="$JAVA_OPTS $CALCULATED_MEMORY" |
| 196 | fi |
| 197 | |
| 198 | # Set MALLOC_ARENA_MAX to reduce memory overhead |
| 199 | export MALLOC_ARENA_MAX=2 |
| 200 | `, calculatorCmd) |
| 201 | |
| 202 | if err := os.WriteFile(memoryCalcScript, []byte(scriptContent), 0755); err != nil { |
| 203 | return fmt.Errorf("failed to write memory calculator script: %w", err) |
| 204 | } |
| 205 | |
| 206 | m.ctx.Log.Debug("Memory Calculator configured") |
| 207 | |
| 208 | return nil |
| 209 | } |
| 210 | |
| 211 | // buildCalculatorCommand builds the memory calculator command with all arguments (v4.x format) |
| 212 | func (m *MemoryCalculator) buildCalculatorCommand() string { |
nothing calls this directly
no test coverage detected