Soramichi's blog

Some seek complex solutions to simple problems; it is better to find simple solutions to complex problems

Some tips to simulate cache behaviors using Intel PIN

I've been working on simulating behaviors of CPU caches using Intel PIN, and faced some tricky things. This post gives ideas on what kind of troubles you may face and some hints to solve the issues.

I used Cache Pintools as a basis and expanded it to really read/write cached data from/to memory (the original version only counts the number of cache misses). However, I believe the same things apply for any other cache simulation code.

Always use PIN_Safecopy() to access application memory

PIN can actually access to the application's data using raw pointers. For example, the code below does work.

VOID InterceptMemRead(VOID* addr) {
    *(int*)addr = 0; // overwrite the data pointed by addr by 0
}

VOID Instruction(INS ins, VOID* v) {
  if (INS_IsMemoryRead(ins)) {
    INS_InsertPredicatedCall(
                ins, IPOINT_BEFORE,  (AFUNPTR)InterceptMemRead,
                IARG_MEMORYREAD_EA,
                IARG_END);
    }
  }
}

int main() {
  ...
  INS_AddInstrumentFunction(Instruction, 0);
  ...
}

If you simulate reads/writes of caches from/to memory, you may need to read somewhere outside of legal address ranges. For example, if the target address of a load is 0x123456, the starting address of the cache line that includes the data is 0x123440 (assume that a cache line is 64-byte long).

Using PIN_Safecopy() solves this issue by guaranteeing a safe return:

The function guarantees safe return to the caller even if the source or destination regions are inaccessible (entirely or partially).

Some instructions implicitly touch memory data

There are some instructions that do not have memory operands as their explicit arguments but do write to/read from memory. Namely, push, pop, call and ret. Note that a call writes the return address to the stack, and a ret reads the return address from the stack.

Therefore, you should NOT write code like

if (INS_IsMemoryRead(ins) &&
    INS_OperandIsReg(ins, 0) &&
    INS_OperandIsMemory(ins, 1)) {
  INS_InsertCall(...);
}

as tools/source/ManualExamples/safecopy.cpp does. You should only use INS_IsMemoryRead(ins) to detect memory loads in order not to miss these implicit memory operations.

Do not use system calls when cache simulation is enabled

This was the thing I spent most of my time to debug. The situation was that my program looked reading some data that it had never written. It turned out that the data was written by system calls, which PIN cannot instrument.

PIN cannot instrument inside system calls by nature because how it works is that it adds some code to the binary of the target application when the application is loaded into the memory space. The executable of system calls (more precisely, the executable inside the OS kernel, but not the wrapper functions of system calls inside libc) is loaded at the boot time of the OS, and there is no chance for PIN to instrument it (it neither has a privilege to do so I guess).

Therefore, if a system call is called, you never know which memory page it reads from/writes to. This is not a serious problem if you only count the number of cache misses, assuming that system calls you use do not pressure the cache that much. However, in order to simulate reads/writes of caches from/to memory, even a single bit of data written by a system call will result in data inconsistency.

A possible way of solving this is to define region(s) of interest inside your program, enable tracking only inside the region(s), and never use system calls inside them. This could be done by adding effect-less pieces of binary at the beginning and the end of your regions of interest and let PIN use them as triggers. Be careful that recent gcc is too clever to delete effect-less binary even if you inject it with asm volatile. In my case, adding a special value to a register and then substituting the same value immediately after it survived gcc optimizations even with -O3.

スイス(チューリッヒ)の食費とスーパー事情

チューリッヒに来て2週間経った。今までに分かっているところでチューリッヒの食費事情を日本より安いものあまり変わらないもの日本より高いもの に分けてまとめる。なおスイスでは物価がそもそも高いので、下で「安い」か「同じくらい」と書いていないものは「高い」に入れていなくても基本的に全て高いと思ってもらえばよい。

来るときの紆余曲折については過去記事を参照(次回入国についてはまだ解決できていない)。 sorami-chi.hateblo.jp

日本より安いもの

チーズ

チーズの種類にもよるが、ものによっては日本の数倍安い。例えば COOP というスーパーでは 300 g のカマンベールチーズが 1.85 フラン(200円ちょい)で売っている。 日本でスーパーに行かないのであまり日本のチーズの値段が分からないけど、雪印のカマンベールチーズが Amazon で 100 g で 450 円だったので、それより6倍安い!日本に住んでいるヨーロッパ人が「日本のチーズは高すぎる」と言っているのを聴いたことがあるけど、なるほど確かにという感じである。

パン

パンも日本より少し安い。パンといっても加工されたサンドイッチ等ではなく、でかい塊のまま売っているものである。これも種類によるが、一番安いものだと 400 g の塊で 2 フランくらいで買える。

水は安いというより「買わない」が正しい。東京だと水道水が美味しくないのでミネラルウォーターを買っている人が多いと思うが、チューリッヒでは湧水やアルプスの雪解け水?がとても綺麗なので、みんな水道水を飲んでいる。職場や外でも再利用したペットボトルとかタンブラーに水道水を(トイレの水道とかから)補充してみんな飲んでいる。

あまり変わらないもの

ペットボトルのジュース

コーラとかファンタとかスイスオリジナルの Rivella というジュースは、500 ml ペットボトルがスーパーで 1.3~1.5 フランで買える(まとめ買いするともっと安い)。日本のコンビニで 150 円くらいなのでほぼ同じくらいである。

ただし、空港や動物園などの観光地価格では異常に高くなる。日本だと観光地価格でも 150 円が180円とか200円になるくらいだと思うが、空港や動物園ではコーラ 500ml が 3.5 フランとかで売っていたりする。

果物

これも物によるが、(スイス基準で)安いものは日本とあまり変わらないように思う。例えばブドウ 2 房(300gくらい?)が3フランくらいだった。しかしリンゴは小さいのが1個で 3.5 フランとかなので高いものは高いようだ。スイスはワインもたくさん作っているらしいのでブドウは安いのかもしれない。

日本より高いもの

外食

外食は異常に高い。こちらの友人の話によるとスイスでは人件費がめちゃくちゃ高いので「調理してもらう」「サーブしてもらう」ことで高くなっているらしい。だいたい1フラン=100円だと思って日本の2倍くらいの値段である。実際には1フランはほぼイコール1ドルなので100円より高いこともある。

例えばチューリッヒに来た次の日に友人と3人で中心街のレストランで bratwurst というソーセージを食べたのだが、ソーセージ + つけあわせのパスタ + ドリンク で3人で 77 フランだった。1人約 25 フランとして、25フランを2500円だと思ってさらに2で割るとちょうどいいくらいの値段である。

f:id:sorami_chi:20190421031942j:plain

また hauptbahnhof(中央駅)のバーガーキングではワッパーのセット(ワッパー、ポテト、ジュース)が 15.9 フランだった。日本のバーガーキングでは 840 円らしいのでこれも 1 フラン = 100円と思ってさらに2で割ると日本の値段くらいになる。

日本食

日本食は普通の外食よりさらに異常に高い。例えば zurich ramen で検索すると出てくる店では担々麺が 24 フランと書いてある(高すぎて行っていないので味は分からない)。また Izakaya では Edamame が 6 フランとかで出てくるようである。

またスーパーでパック寿司などを売っているがこれもめちゃくちゃ高い。例えばマグロとサーモン2貫ずつ(計4個)と鉄火巻きのパックが 13 フランなどで売っている。スーパーで売っている寿司がどんな味がするか試しに買ってみたいのだが、あまりにも高くてまだ挑戦できていない。

スーパー事情

チューリッヒにはおもに COOP と Migro という二つのスーパーがある。COOP の方がたくさんあって、多いところでは二つの COOP が100メートルくらいしか離れていないこともある。朝7時からあいているので朝食を買うのにもよい。また COOP の方が賞味期限が近いものを安売りしていることが多い気がする。別に夜遅くではなくても 50% オフのソーセージが置いてあったりするので、これで食費を節約できる。

スーパーでは有人レジと無人レジがあって、日本とは違って無人レジをみんなちゃんと使っている。日本のコンビニだと有人レジは並んでいても無人レジは誰も使っていないことがあるが、それとは違うようである。基本的に全てクレジットカードで払うが、アメリカであるような "cash back" (Nドルの買い物に対して N + M ドルをカードで払って、余分の M ドルを現金でもらうことで ATM に行かなくても現金が手に入る)はできないようである。

ドイツ経由スイスへの長期滞在で危うく入国できず詰みかけた話

今月から半年間スイスに長期滞在している。 成田 - チューリッヒの直行便は1日1本だけあるが時間が悪いのか値段が高すぎたのか検索で出てこなかったのでドイツ経由のチケットを取ったら、あやうく入国できず詰むところだったので誰か(自分含む)が同じミスを犯さないようにメモしておく。

前提知識

まず、スイスとドイツはともにシェンゲンエリアに入っている。 シェンゲンエリア内では人や物が自由に移動できることになっていて、エリア内の移動にパスポート検査や関税はない。 さらに、エリア外からエリア内に入る場合は最初に入国する国でパスポート検査をするらしい。 つまり、日本からドイツ経由でスイスに行く場合はドイツで入国審査を受ける

また、シェンゲンエリアの国に日本パスポート保有者が観光や仕事(現地でお金を稼ぐわけではなく、日本の組織にやとわれた身としていく場合)は 90 日以内はビザなしで滞在できる。逆に 90 日を超える場合はなんらかの許可が必要で、今回はこれにあたる

なおシェンゲンエリアはEUとは微妙に違う。スイスはEUには入っていないがシェンゲンエリアには入っている。逆にイギリスはEUにはまだ入っているがシェンゲンエリアには元々入っていないので、ユーロスタードーバー海峡を越えるときにはパスポート検査がある。

持っていたもの

スイスに長期滞在するにあたり、自分が持っていたものは以下。

詰みかけた経緯

羽田でチェックインの際に長期滞在なのでビザを持っているか聞かれ、上記のようにお金をもらって働くわけではないのでチューリッヒの滞在許可しか持ってないがこれ以外に取得できないのでこれでいけるはずと伝えた。 すると、スイスに直行の便なら問題ないが、フランクフルト経由でドイツで入国審査をするのでもしかしたら拒否されるかもしれないとのこと。さらに念のため確認しますといってフランクフルト空港に電話で問い合わせたところ、ダメとの回答を得たらしい。ここで一旦詰んだかと思った

話をまとめると次のようなことだった。

  • チューリッヒの滞在許可証を持っているので、スイスの入国は問題ない(とフランクフルト空港にも言われたらしい)。従って、直行便かシェンゲンエリアの国以外での乗り継ぎなら問題ない。
  • しかし今回はドイツで入国審査があり、ドイツはシェンゲン国の中でも最近特に入国審査が厳しくこれでははじかれる。
  • シェンゲンエリアの国でもドイツ以外での乗り換えならいける「かも」しれない。

同じシェンゲンエリアの国なのに特に厳しいとかいけるかもとかの違いがなぜ存在するのか分からないが、フランクフルト空港に電話で聞いてダメと言われたなら少なくともドイツ経由でダメなことは間違いなさそうである。

確かにチケットを買うときに直行とかシェンゲンエリア以外で乗り継ぎにしてスイスで入国審査が行われるようにした方がいいなかとは思ったが、スイスに直行便がない国もあるわけだしこれでいけるだろうと思っていた。

助かった経緯

これまでのところ唯一の手はフランクフルト経由をキャンセルして他の便を取り直すことである。実際に羽田でも最初はそれを薦められた。なおANAでは渡航書類のトラブルで旅行ができない場合には全額払い戻しされるらしい。ただ、今回は旅行会社経由でとっているのでANAでは直接手続きができないとのことだった。

キャンセルしようにも旅行会社が土日で電話がかからない、職場も土日で指示をあおげないので途方にくれていたところ、チケット2の存在を思い出した。チケット2は 6/20 に戻ることになっており、90 日以内なのでノービザ扱いで入国できるのでは?と思い聞いたところ、これでいけるとのことだった(ただし 6/20 以降の旅程については別途考慮が必要である)。

実際にフランクフルトのパスポート検査でも 6/20 に戻ると言ったら隣の検査官となにやら会話していて(たぶん90日以内だよね?と確認していたと思われる)、帰りのチケットを見せろと言われたので 6/20 のチューリッヒ発成田行きの eTicket を見せたら無事に入国できた。

今後の展開

ひとまず無事にフランクフルトで入国できスイスに到着できた。法的にもこちらでお金を稼ぐわけではないので 6/20 まではこの状態で全く問題ないはずである。

しかし、90 日以内ノービザシステムは正確には 180 日の間で 90 日以内 なので、次回はこの手は使えない。 チューリッヒに到着後 14 日以内に居住者登録をすることになっていて、こちらの人の話によるとそれをしたあとは自由に入出国できるそうなので、その登録証のようなものが他のシェンゲンエリアの国でも通用すればOKのはずである。 もしダメそうなら他のチケットをいったんキャンセルして取り直すか、次回はウィーン経由で厳しいドイツではないのでいけると祈るしかない。

今回の教訓:シェンゲンエリアの国に長期滞在するときには、直行便かシェンゲンエリアではない国で乗り継ぐことで長期滞在する目的の国で入国審査が行われるようにする

ちなみに似たような話として、予定を決めない放浪の旅をする人が入国審査で帰りが 90 日以内であることを証明しないとはじかれることを避けるために、片道のチケットを「貸す」サービスがあるらしい。 でもこれはたぶん法的にアウトな気がする・・・ 7 Ways To Provide Proof of Onward Travel - Goats On The Road

List of top conferences named "Symposium"

Some people occasionally judge the quality of international conferences based on their names, saying that "symposium"s are not as proper as "conference"s. I heard that it is the case in some areas of study, but it is definitely not true in research areas I've been involved in and nearby fields.

So, here I list up top international conferences that have "symposium" on their names, so that I can give the list when someone says the same thing in the future. The list only shows real top conferences, but not top-ish ones.

  • Operating Systems / System Software

    • ACM Symposium on Operating Systems Principles (SOSP)
    • USENIX Symposium on Operating Systems Design and Implementation (OSDI)
    • USENIX Symposium on Networked Systems Design and Implementation (NSDI)
  • Parallel Computing

    • ACM Symposium on High-Performance Parallel and Distributed Computing (HPDC)
    • IEEE International Parallel & Distributed Processing Symposium (IPDPS)
  • Computer Architecture

    • IEEE/ACM International Symposium on Microarchitecture (Micro)
    • International Symposium on Computer Architecture (ISCA)
    • IEEE International Symposium on High-Performance Computer Architecture (HPCA)
  • Programming Language

    • ACM SIGPLAN Symposium on Principles of Programming Languages (POPL)
  • Security

    • IEEE Symposium on Security and Privacy (IEEE S&P)
    • USENIX Security Symposium (USENIX Security)

A side note for those who have never heard of "USENIX"; it is an academic organization that hosts international conferences and related events (like ACM and IEEE) especially for the operating systems and system software fields. It is considered as a highly reliable organization and the conferences it hosts are normally very high quality.

IPSJ accepted to delete my sex info from their membership DB

Information Processing Society of Japan (IPSJ) is what they claim the largest academic organization related to Informatics in Japan. I've been a member of it since 2009 when I was a 4th year undergrad student, I've participated in many domestic workshops and events they host, and I've received a few awards from them.

One thing I was concerned about them, is that they require to register one's sex (choosing either from man or woman) along with one's name and afflication and other information when someone joins IPSJ.

Because I wanted to choose a third option "prefer not to answer" (which I believe must be choosable) due to two reasons below, I asked them to add it to the sex column of the registration form.

  • Reason 1: I cannot imagine a single reason to require registering one's sex to receive their membership service.
  • Reason 2: Registering your sex is not only like you declare you have a particular gene, but it makes people see you with largely stereotyped ways.

I actually didn't expect a positive answer, because as an IT person I know that modifying even a single bit of an enterprise system costs tremendous amount of money and time (like 100s of 1000s of dollars, literally).

Turned out, my expectation was wrong in a good way. Their response was that they deleted my sex information from their membership DB and they also said that they would conisider adding a new option ("prefer not to answer") in the future. It was very nice of them to have (halfway) accepted my request, and I belive they will do the right thing in near future.

Figure: a screenshot of my membership info in their portal. The sex ("性別") column is blank. f:id:sorami_chi:20181113005533j:plain

Ochanomizu University to accept non-women

Ochanomizu University to accept non-women who recognize themselves as female in addition to any women (including ones recognizing themselves as male and neutral). I think it's a very large step, and I want them to keep going but not stop here.

Note for those who are not familiar with Japanese universities: it used to be a women-only university but will introduce a new policy from next year.

In the short term, it is strongly needed to support students accepted by the new policy not only in the school but also around it. Do all apartment owners accept these students? I don't think so. Do all stations in Tokyo have universal restrooms? No they don't. I hope this new policy will have an impact to the society around the school as well.

In the long term, I believe now in this information era is the time to re-consider the long established concept of male vs. female. Why do we need to distinguish the two and map every single person to either of them? Why should the biological differences spiritually affect the way of thinking? How can we find a compromise in the real world where some do not wanna be distinguished (not discriminated) but others do feel comfortable being categorized? I hope this new policy will be a trigger to initiate constructive discussions, but not meaningless quarrels as usually seen in equality-related stuff.

Public Walfare (for the Constitution Memorial Day of Japan)

The Japanese constitution has several clauses that have the phrase "for the public welfare" in them. Basically what it says is that the fundamental human rights are respected if (and only if) they do not conflict with the "public welfare".

I have been long thinking that this "public welfare" is too vague so that it can be used to condemn almost anything, because there is actually no definition in the constitution. The vagueness may not be a problem if the people are culturally, religiously, morally and biologically "uniform", but obviously it is not the case. In an extreme case, if say 99% of people think that something is ugly, then that thing can be criminalized in order to promote the public welfare (people actually claim that things they hate must be criminalized and they often cite public welfare, although making something really criminalized is rarely done in my understanding).

It turned out that, this vagueness has actually been pointed out by the United Nations as well. The International Covenant on Civil and Political Rights of the United Nations had a 'concern that the concept of "public welfare" is vague and open-ended' (page 8 of http://tbinternet.ohchr.org/_layouts/treatybodyexternal/Download.aspx?symbolno=CCPR/C/JPN/CO/6&Lang=En ). There is a very good read (in Japanese) written by a professor of Osaka Sangyo University.

I guess there are two issues about this. First, people really do not care about it because for the majority of people who share the same way of thinking, something against the public welfare is what they do not want. So, condemning something against the public welfare is actually good for them! It might be the case that people do not even notice that the phrase "public welfare" is vague. Second, having a constructive and objective discussion on how to change the constitution is very difficult in Japan, because people are paranoid about the Clause No. 9 that says about the wars and forces (and I am too).