<commit_before>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <os>
#include <net/inet4>
#include <net/dhcp/dh4client.hpp>
#include <math.h> // rand()
#include <sstream>
#include <timers>

using namespace std::chrono;


std::string header(int content_size) {
  std::string head="HTTP/1.1 200 OK \n "                \
    "Date: Mon, 01 Jan 1970 00:00:01 GMT \n"            \
    "Server: IncludeOS prototype 4.0 \n"                \
    "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT \n"     \
    "Content-Type: text/html; charset=UTF-8 \n"            \
    "Content-Length: "+std::to_string(content_size)+"\n"   \
    "Accept-Ranges: bytes\n"                               \
    "Connection: close\n\n";

  return head;
}

std::string html() {
  int color = rand();
  std::stringstream stream;

  /* HTML Fonts */
  std::string ubuntu_medium  = "font-family: \'Ubuntu\', sans-serif; font-weight: 500; ";
  std::string ubuntu_normal  = "font-family: \'Ubuntu\', sans-serif; font-weight: 400; ";
  std::string ubuntu_light  = "font-family: \'Ubuntu\', sans-serif; font-weight: 300; ";

  /* HTML */
  stream << "<html><head>"
         << "<link href='https://fonts.googleapis.com/css?family=Ubuntu:500,300' rel='stylesheet' type='text/css'>"
         << "</head><body>"
         << "<h1 style= \"color: " << "#" << std::hex << (color >> 8) << "\">"
         <<  "<span style=\""+ubuntu_medium+"\">Include</span><span style=\""+ubuntu_light+"\">OS</span> </h1>"
         <<  "<h2>Now speaks TCP!</h2>"
    // .... generate more dynamic content
         << "<p> This is improvised http, but proper suff is in the works. </p>"
         << "<footer><hr /> &copy; 2016, IncludeOS AS @ 60&deg; north </footer>"
         << "</body></html>\n";

  std::string html = stream.str();

  return html;
}

const std::string NOT_FOUND = "HTTP/1.1 404 Not Found \n Connection: close\n\n";

uint64_t TCP_BYTES_RECV = 0;
uint64_t TCP_BYTES_SENT = 0;

void Service::start(const std::string&)
{
  using namespace util::literals;
  // Allocation / free spam to warm up
  auto initial_memuse =  OS::heap_usage();
  printf("Current memory usage: %zi b, (%s) \n", initial_memuse, util::Byte_r(initial_memuse).to_string().c_str());


  std::array<void*, 10> allocs {};
  auto chunksize = 1024 * 1024 * 5;
  printf("Exercising heap: incrementally allocating %zi x %i bytes \n",
         allocs.size(), chunksize);

  for (auto& ptr : allocs) {
    ptr = malloc(chunksize);
    Expects(ptr != nullptr);
    auto memuse = OS::heap_usage();
    printf("Current memory usage: %zi b, (%s) \n", memuse, util::Byte_r(memuse).to_string().c_str());
    Expects(memuse > initial_memuse);
  }

  auto high_memuse = OS::heap_usage();
  Expects(high_memuse >= (chunksize * allocs.size()) + initial_memuse);

  auto prev_memuse = high_memuse;

  printf("Deallocating \n");
  for (auto& ptr : allocs) {
    free(ptr);
    auto memuse = OS::heap_usage();
    printf("Current memory usage: %zi b, (%s) \n", memuse, util::Byte_r(memuse).to_string().c_str());
    Expects(memuse < high_memuse);
    Expects(memuse < prev_memuse);
    prev_memuse = memuse;
  }<commit_msg>test: prevent optimizing out mallocs in stress test<commit_after>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <os>
#include <net/inet4>
#include <net/dhcp/dh4client.hpp>
#include <math.h> // rand()
#include <sstream>
#include <timers>

using namespace std::chrono;


std::string header(int content_size) {
  std::string head="HTTP/1.1 200 OK \n "                \
    "Date: Mon, 01 Jan 1970 00:00:01 GMT \n"            \
    "Server: IncludeOS prototype 4.0 \n"                \
    "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT \n"     \
    "Content-Type: text/html; charset=UTF-8 \n"            \
    "Content-Length: "+std::to_string(content_size)+"\n"   \
    "Accept-Ranges: bytes\n"                               \
    "Connection: close\n\n";

  return head;
}

std::string html() {
  int color = rand();
  std::stringstream stream;

  /* HTML Fonts */
  std::string ubuntu_medium  = "font-family: \'Ubuntu\', sans-serif; font-weight: 500; ";
  std::string ubuntu_normal  = "font-family: \'Ubuntu\', sans-serif; font-weight: 400; ";
  std::string ubuntu_light  = "font-family: \'Ubuntu\', sans-serif; font-weight: 300; ";

  /* HTML */
  stream << "<html><head>"
         << "<link href='https://fonts.googleapis.com/css?family=Ubuntu:500,300' rel='stylesheet' type='text/css'>"
         << "</head><body>"
         << "<h1 style= \"color: " << "#" << std::hex << (color >> 8) << "\">"
         <<  "<span style=\""+ubuntu_medium+"\">Include</span><span style=\""+ubuntu_light+"\">OS</span> </h1>"
         <<  "<h2>Now speaks TCP!</h2>"
    // .... generate more dynamic content
         << "<p> This is improvised http, but proper suff is in the works. </p>"
         << "<footer><hr /> &copy; 2016, IncludeOS AS @ 60&deg; north </footer>"
         << "</body></html>\n";

  std::string html = stream.str();

  return html;
}

const std::string NOT_FOUND = "HTTP/1.1 404 Not Found \n Connection: close\n\n";

uint64_t TCP_BYTES_RECV = 0;
uint64_t TCP_BYTES_SENT = 0;

void print_memuse(uintptr_t u) {
  auto end = OS::heap_end();
  printf("Current memory usage: %s (%zi b) heap_end: 0x%zx lstack chunks: (%s)\n",
         util::Byte_r(u).to_string().c_str(), u, end, util::Byte_r(end).to_string().c_str());
}

void Service::start(const std::string&)
{
  using namespace util::literals;
  // Allocation / free spam to warm up
  auto initial_memuse =  OS::heap_usage();
  print_memuse(initial_memuse);

  std::array<volatile void*, 10> allocs {};
  auto chunksize = 1024 * 1024 * 5;
  printf("Exercising heap: incrementally allocating %zi x %i bytes \n",
         allocs.size(), chunksize);

  for (auto& ptr : allocs) {
    ptr = malloc(chunksize);
    Expects(ptr != nullptr);
    memset((void*)ptr, '!', chunksize);
    for (char* c = (char*)ptr; c < (char*)ptr + chunksize; c++)
      Expects(*c == '!');
    printf("Allocated area: %p heap_end: %p\n", (void*)ptr, (void*)OS::heap_end());
    auto memuse = OS::heap_usage();

    print_memuse(memuse);
    Expects(memuse > initial_memuse);
  }

  auto high_memuse = OS::heap_usage();
  Expects(high_memuse >= (chunksize * allocs.size()) + initial_memuse);

  auto prev_memuse = high_memuse;

  printf("Deallocating \n");
  for (auto& ptr : allocs) {
    free((void*)ptr);
    auto memuse = OS::heap_usage();
    print_memuse(memuse);
    Expects(memuse < high_memuse);
    Expects(memuse < prev_memuse);
    prev_memuse = memuse;
  }
{'fraction_non_alphanumeric': 0.1658215010141988, 'fraction_numerical': 0.022946247464503043, 'mean_word_length': 3.1260460251046025, 'pattern_counts': {'":': 0, '<': 116, '<?xml version=': 0, '>': 67, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2815247', 'n_tokens_mistral': 2824, 'n_tokens_neox': 2584, 'n_words': 735}