static void write_status_page(BufferFiller &response) { char buffer[32]; response.emit_p(PSTR( "HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\n" "Pragma: no-cache\r\n" "\r\n" "<meta http-equiv='refresh' content='1'/>" "<title>Jystic NTP Server v0.1</title>")); print_gps_time(buffer); response.emit_p(PSTR("<h1>UTC $S<h1>"), buffer); response.emit_p(PSTR("Running $S\n"), print_ms_time(millis(), buffer)); response.emit_p(PSTR("Free mem $D\n"), get_free_memory() ); uint32_t time_since_pps = micros() - g_pps_time; response.emit_p(PSTR("Since PPS $S\n"), print_ms_time(time_since_pps/1000, buffer)); }
void LocaleSharedMemory::deallocate( void * ptr ) { try { segment.deallocate( ptr ); } catch(...){ LOG(ERROR) << "Deallocation of " << ptr << " failed with " << get_free_memory() << " free."; failure_function(); throw; } }
void * LocaleSharedMemory::allocate_aligned( size_t size, size_t alignment ) { void * p = NULL; try { p = segment.allocate_aligned( size, alignment ); allocated += size; } catch(...){ LOG(ERROR) << "Allocation of " << size << " bytes with alignment " << alignment << " failed with " << get_free_memory() << " free and " << allocated << " allocated locally"; failure_function(); throw; } return p; }
friend std::ostream& operator<< (std::ostream& os, GenericSharedMemory& sh_memory) { auto segment = sh_memory.segment; os << std::endl; os << "! Basic Shared Memory Diagnostic Information for \"" << sh_memory.shared_region_name << "\":" << std::endl; os << "!\tSize of the memory segment: " << segment->get_size() << std::endl; os << "!\tFree bytes in the memory segment: " << segment->get_free_memory() << std::endl; os << "!\tNumber of named objects in the memory segment: " << segment->get_num_named_objects() << std::endl; os << "!\tNumber of unique objects in the memory segment: " << segment->get_num_unique_objects() << std::endl; os << std::endl; return os; }
/** * Check if we should abort whatever we ware doing right now. * Returns 0 if we should not abort, ABORTED if we should abort. * * If receive_resume is set to true, 'RESUME' is a valid cmd as well. */ errv_t check_aborted(bool receive_resume) { int ret = 0; if (Serial.available() > 0) { char cmd[8+1]; // The conversion to String depends on having a trailing NULL! memset(cmd, 0, 8+1); // TODO this is not nice, other parsing code is in loop() // move to one function somehow? // XXX why do we use readBytes here and not readBytesUntil as in // sketch.ino? Why 8 bytes and not MAX_COMMAND_LENGTH? // --> introduced in commit 612367ef if(Serial.readBytes(cmd, 8)) { String cmd_str = String(cmd); if (cmd_str.equals("ABORT\r\n")) { ret = ABORTED; } else if (receive_resume && cmd_str.equals("RESUME\r\n")) { DEBUG_MSG_LN(String("Free mem: ") + String(get_free_memory())); return RESUMED; } else { ERROR(strerror(INVALID_COMMAND)); DEBUG_MSG_LN(String("check_aborted: got '") + String(cmd) + String("'")); } } // To save some bytes commenting out: //else { // // we check above Serial.available() > 0 --> can we get here? // DEBUG_MSG_LN("check_aborted: something went wrong"); //} } else if (digitalRead(ABORT_BTN_PIN) == LOW) { // pull up inverts logic! ret = ABORTED; } else if (receive_resume && digitalRead(RESUME_BTN_PIN) == LOW) { return RESUMED; } return ret; }
int is_system_unstable( long long last_fault_count, long long this_fault_count) { struct sysinfo info; int error = sysinfo(&info); if (error) return 0; /* Get free RAM. */ unsigned long memtotal, memfree; get_free_memory(&memtotal, &memfree); double free_ram = (memfree / (double)memtotal); /* Get number of faults. */ long long faults = 0; if (last_fault_count > 0) faults = (this_fault_count - last_fault_count); /* Get system load. */ double system_load = info.loads[0] / (double)LINUX_SYSINFO_LOADS_SCALE; #if DEBUG /* Print information. */ printf("[RAM: %5.1lf] [LOAD: %5.1lf] [FAULTS: %5lld]\n", free_ram * 100.0, system_load, faults); #endif /* Determine if the system is unstable. */ if (free_ram > DANGEROUS_FREE_RAM) return 0; if (system_load < DANGEROUS_LOAD) return 0; if (faults < DANGEROUS_FAULTS_PER_SECOND * SLEEP_TIME && system_load < VERY_DANGEROUS_LOAD) return 0; return 1; }
int main(void){ //while(1); wdt_disable(); ///Configure the Torquer configure_torquer(); char buf[100]; uint16_t v; int ao; for(ao=0;ao<1;ao++) _delay_ms(1000); /// Initialise Interfaces - UART of Magnetometer and GPS and the SPI bus init_UART_MM(); init_UART_GPS(); init_SPI(); init_TWI(); send_preflight("Master\r", 7); ///Set Preflight pin as input cbi(DDR_PF, PIN_PF); ///* Switch on Global interrupts sei(); ///Check if Preflight Pin is high while(1){ ///* * Reset timer for 2 seconds timer_reset_two_sec(); //get_HM_data(); //send_preflight("Power\r", 6); ///* Preflight Checks if(0){ ///* * Set the mode as preflight Mode = PREFLIGHT; /*slave_send(HM_DATA, "Hello", 5); _delay_ms(10); slave_send (BEGIN_TX_GS, NULL, 0); //power_up_peripheral(PCC); _delay_ms(10); ao = init_CC1020(); if(ao) send_preflight("CC Init\r", 8); else send_preflight("No Init\r", 8); while(1);*/ /*power_up_peripheral(PGPS); while(1) { read_GPS(); while(UCSR0B & _BV(RXCIE0)); copy_gps_reading(); sprintf(buf,"%ld %ld %ld\r", Current_state.gps.x/1000, Current_state.gps.y/1000, Current_state.gps.z/1000); send_preflight(buf, strlen(buf)); sprintf(buf,"%ld %ld %ld\r", Current_state.gps.v_x/1000, Current_state.gps.v_y/1000, Current_state.gps.v_z/1000); send_preflight(buf, strlen(buf)); _delay_ms(1000); }*/ ///* * Magnetometer and Torquer test ///* * Reading with one torquer on at once /*Current_state.pwm.x_dir = 0; Current_state.pwm.x = 10000; Current_state.pwm.y_dir = 0; Current_state.pwm.y = 30000; Current_state.pwm.z_dir = 1; Current_state.pwm.z = 20000; send_preflight("Read\r", 5); set_PWM();*/ while(1) { read_SS(); //send_preflight((char *)&Current_state.ss, sizeof(struct SS_reading)); for(v = 0; v < 6; v++) { sprintf(buf,"%u\r", (uint16_t)(((float)Current_state.ss.reading[v])*1.6*100/4096)); send_preflight(buf, strlen(buf)); sprintf(buf,"%x\r", Current_state.ss.reading[v]); send_preflight(buf, strlen(buf)); } _delay_ms(1000); } ///* * Set Torquer values to zero reset_PWM(); while(1) read_MM(); read_GPS(); send_preflight((char *)&Current_state.gps, sizeof(struct GPS_reading)); ///* * Sunsensor test read_SS(); send_preflight((char *)&Current_state.ss, sizeof(struct SS_reading)); ///* Health Montoring get_HM_data(); send_preflight((char *)&Current_state.hm, sizeof(struct HM_data)); ///Communication Task comm(); ///* * Wait for 2 seconds to get over timer_wait_reset(); } ///Normal Mode else{ ///* Set default mode of Satellite Mode = DETUMBLING; ///* initialise Timer Time = 0; ///Loop begins while(1){ //Fuses Pain, Nominal Mode checking send_preflight("Loop\r", 5); control(); send_preflight("Control\r", 8); //power(); comm(); send_preflight("Comm\r", 5); v = StackCount(); sprintf(buf, "Stack = %d\r", v); send_preflight(buf, strlen(buf)); v = get_free_memory(); sprintf(buf, "Stack(Method 2) = %d\r", v); send_preflight(buf, strlen(buf)); Time += FRAME_TIME; timer_wait_reset(); } } } return 0; }
void __noreturn master_init(void) { __attribute__ ((aligned(16))) uint8_t bootstrap_pool[BOOTSTRAP_POOL_SIZE]; jump_handlers_apply(); kputs("KERN: We are in high address.\n"); arch_init(); /* * Page allocator requires arbitrary size allocation to allocate * struct pages, while arbitrary size allocation depends on * page allocator to actually give out memory. * * We break such circular dependency by * (1) bootstrap a small virtual memory allocator which works on the * stack. * (2) initialize a page allocator which works on the bootstrap * allocator obtained in (1). * (3) initialize a real virtual memory allocator which depend * on (2). * (4) make the page allocator depend on (3) instead. * * TODO: move the following piece of code to kern/mm */ simple_allocator_bootstrap(bootstrap_pool, BOOTSTRAP_POOL_SIZE); kputs("KERN: Simple allocator bootstrapping.\n"); page_allocator_init(); kputs("KERN: Page allocator initialized.\n"); add_memory_pages(); kputs("KERN: Pages added.\n"); kprintf("KERN: Free memory: 0x%p\n", (size_t)get_free_memory()); struct simple_allocator old; get_simple_allocator(&old); simple_allocator_init(); kputs("KERN: Simple allocator initialized.\n"); page_allocator_move(&old); kputs("KERN: Page allocator moved.\n"); trap_init(); kputs("KERN: Traps initialized.\n"); /* temporary test */ extern void trap_test(void); trap_test(); kputs("KERN: Traps test passed.\n"); /* do early initcalls, one by one */ do_early_initcalls(); mm_init(); kputs("KERN: Memory management component initialized.\n"); extern void mm_test(void); mm_test(); /* allocate per-cpu context and kworker */ // proc_init(); /* do initcalls, one by one */ do_initcalls(); /* temporary tests */ struct allocator_cache cache = { .size = 1024, .align = 1024, .flags = 0, .create_obj = NULL, .destroy_obj = NULL }; cache_create(&cache); void *a, *b, *c; a = cache_alloc(&cache); kprintf("DEBUG: a = 0x%08x\n", a); b = cache_alloc(&cache); kprintf("DEBUG: b = 0x%08x\n", b); c = cache_alloc(&cache); kprintf("DEBUG: c = 0x%08x\n", c); cache_free(&cache, a); cache_free(&cache, b); cache_free(&cache, c); a = cache_alloc(&cache); kprintf("DEBUG: a = 0x%08x\n", a); cache_free(&cache, a); int ret = cache_destroy(&cache); kprintf("DEBUG: cache_destroy returned %d.\n", ret); cache_create(&cache); a = cache_alloc(&cache); kprintf("DEBUG: a = 0x%08x\n", a); /* startup smp */ /* * do initcalls, one by one. * They may fork or sleep or reschedule. * In case any initcalls issue a fork, there MUST be EXACTLY one return * from each initcall. */ /* initialize or cleanup namespace */ panic("Test done, all is well.\n"); } void __noreturn slave_init(void) { panic("Unimplemented routine called."); }
int pl_rewind_init(pl_rewind *rewind, int (*save_state)(void *), int (*load_state)(void *), int (*get_state_size)()) { float memory_needed = (float)get_free_memory() * 0.85; int state_data_size = get_state_size(); int state_count = (int)(memory_needed / (float)(state_data_size + sizeof(rewind_state_t))); if (state_count < 1) return 0; /* First state */ rewind_state_t *prev, *curr = NULL; if (!(rewind->start = (rewind_state_t*)malloc(sizeof(rewind_state_t)))) return 0; if (!(rewind->start->data = malloc(state_data_size))) { free(rewind->start); rewind->start = NULL; return 0; } prev = rewind->start; /* The rest */ int i; for (i = 1; i < state_count; i++) { /* If allocation fails, compose a shorter state chain */ if (!(curr = (rewind_state_t*)malloc(sizeof(rewind_state_t)))) { state_count = i + 1; break; } /* If allocation fails, compose a shorter state chain */ if (!(curr->data = malloc(state_data_size))) { state_count = i + 1; free(curr); break; } prev->next = curr; curr->prev = prev; prev = curr; } /* Make circular */ rewind->start->prev = prev; curr->next = rewind->start; /* Init structure */ rewind->current = NULL; rewind->save_state = save_state; rewind->load_state = load_state; rewind->get_state_size = get_state_size; rewind->state_data_size = state_data_size; rewind->state_count = state_count; return 1; }
void printMemoryProfile(unsigned long int delayMillis) // run over and over again { print_p( PSTR("\n\n--------------------------------------------") ); print_p( PSTR("\n\nget_free_memory() reports [") ); Serial.print(get_free_memory()); print_p( PSTR("] (bytes) which must be > 0 for no heap/stack collision") ); // print_p( PSTR("\n\nSP should always be larger than HP or you'll be in big trouble!") ); check_mem(); print_p( PSTR("\nheapptr=[0x") ); Serial.print( (int) heapptr, HEX); print_p( PSTR("] (growing upward, ") ); Serial.print( (int) heapptr, DEC); print_p( PSTR(" decimal)") ); print_p( PSTR("\nstackptr=[0x") ); Serial.print( (int) stackptr, HEX); print_p( PSTR("] (growing downward, ") ); Serial.print( (int) stackptr, DEC); print_p( PSTR(" decimal)") ); print_p( PSTR("\ndiff=stackptr-heapptr, diff=[0x") ); diff=stackptr-heapptr; Serial.print( (int) diff, HEX); print_p( PSTR("] (which is [") ); Serial.print( (int) diff, DEC); print_p( PSTR("] (bytes decimal)") ); //---------------- Print memory profile ----------------- print_p( PSTR("\n\n__data_start=[0x") ); Serial.print( (int) &__data_start, HEX ); print_p( PSTR("] which is [") ); Serial.print( (int) &__data_start, DEC); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\n__data_end=[0x") ); Serial.print((int) &__data_end, HEX ); print_p( PSTR("] which is [") ); Serial.print( (int) &__data_end, DEC); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\n__bss_start=[0x") ); Serial.print((int) & __bss_start, HEX ); print_p( PSTR("] which is [") ); Serial.print( (int) &__bss_start, DEC); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\n__bss_end=[0x") ); Serial.print( (int) &__bss_end, HEX ); print_p( PSTR("] which is [") ); Serial.print( (int) &__bss_end, DEC); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\n__heap_start=[0x") ); Serial.print( (int) &__heap_start, HEX ); print_p( PSTR("] which is [") ); Serial.print( (int) &__heap_start, DEC); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\n__malloc_heap_start=[0x") ); Serial.print( (int) __malloc_heap_start, HEX ); print_p( PSTR("] which is [") ); Serial.print( (int) __malloc_heap_start, DEC); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\n__malloc_margin=[0x") ); Serial.print( (int) &__malloc_margin, HEX ); print_p( PSTR("] which is [") ); Serial.print( (int) &__malloc_margin, DEC); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\n__brkval=[0x") ); Serial.print( (int) __brkval, HEX ); print_p( PSTR("] which is [") ); Serial.print( (int) __brkval, DEC); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\nSP=[0x") ); Serial.print( (int) SP, HEX ); print_p( PSTR("] which is [") ); Serial.print( (int) SP, DEC); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\nRAMEND=[0x") ); Serial.print( (int) RAMEND, HEX ); print_p( PSTR("] which is [") ); Serial.print( (int) RAMEND, DEC); print_p( PSTR("] bytes decimal") ); summaries: ramSize = (int) RAMEND - (int) &__data_start; dataSize = (int) &__data_end - (int) &__data_start; bssSize = (int) &__bss_end - (int) &__bss_start; heapSize = (int) __brkval - (int) &__heap_start; stackSize = (int) RAMEND - (int) SP; freeMem1 = (int) SP - (int) __brkval; freeMem2 = ramSize - stackSize - heapSize - bssSize - dataSize; print_p( PSTR("\n--- section size summaries ---") ); print_p( PSTR("\nram size=[") ); Serial.print( ramSize, DEC ); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\n.data size=[") ); Serial.print( dataSize, DEC ); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\n.bss size=[") ); Serial.print( bssSize, DEC ); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\nheap size=[") ); Serial.print( heapSize, DEC ); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\nstack size=[") ); Serial.print( stackSize, DEC ); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\nfree size1=[") ); Serial.print( freeMem1, DEC ); print_p( PSTR("] bytes decimal") ); print_p( PSTR("\nfree size2=[") ); Serial.print( freeMem2, DEC ); print_p( PSTR("] bytes decimal") ); delay(delayMillis); }